• ajax上传图片及跨域解决办法(未测试ie)


    1.ajax上传图片

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>Document</title>
    	<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
    </head>
    <body>
    	<div id="uploadForm">
    		<input id="file" type="file" name="userfile"/>
    		<button id="upload" type="button">upload</button>
    	</div>
    </body>
    </html>
    <script type="text/javascript" >
    
    $(function () {
    	$("#upload").click(function () {
    		var formData = new FormData();
    		formData.append('file', $('#file')[0].files[0]);
    		$.ajax({
    			url: 'http://xxx/uploadFile',
    			type: 'POST',
    			cache: false,
    			data: formData,
    			processData: false,
    			contentType: false
    		}).done(function(res) {
    		}).fail(function(res) {});
    	});
    });
     
    </script>

    2.ajax跨域

    在服务器接收时,只需在response添加响应头Header("Access-Control-Allow-Origin", "*")即可

    如:

    java:

    @RequestMapping(value = "/uploadFile")
    @ResponseBody
    public String saveApplication(HttpServletRequest request, HttpServletResponse response) {
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	String result = "success";
    	FileUpload fileUpload = new FileUpload();
    	UserFile userFile = fileUpload.fileUpload("file", request, response);
    	System.out.println(userFile);
    	return result;
    }
    ===================================================================

    ajax跨域解决,方式2,jsonp方式

    前台:

    <!DOCTYPE html>
    <html>
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    	<script type="text/javascript" src="http://bjzx.xdf.cn:80/scholarship/js/js/jquery-1.10.2.min.js"></script>
    </head>
    <body>
         
    </body>
    <script>    
        $(function() {
    		$.ajax({
    		   type : 'get',
    		   async: false,
    		   url : 'http://bjmx.xdf.cn/huodong/middleSchoolActivity',
    		   dataType : 'jsonp',
    		   jsonp: 'jsonpCallback',
    		   jsonpCallback:'jsonpCallback',//让后台jsonpCallback参数固定为所填的值,否则是随字字符串
    		   success : function(result){
    			   console.log(result.result);
    			   console.log(result.data);
    		   },
    		   complete: function(XMLHttpRequest, textStatus){
    			
    		   },
    		   error : function(json,XMLResponse){
    			 result = "返回信息:" + json + "错误码:" + XMLHttpRequest.status;
    			 console.log("错误信息:" + result);
    		   }
    		});
    	});
    </script>
    </html>

    后台,java springmvc

    @RequestMapping(value = "/xxx")
    @ResponseBody
    public String xxx (String jsonpCallback) {
    	Map<String, Object> dataMap = new HashMap<String, Object>();
    	String result = "success";
    	try {
    		
    		List<Map<String, Object>> activityList = new ArrayList<Map<String,Object>>();
    		//TODO
    		dataMap.put("data", activityList);
    	}
    	catch (NumberFormatException e) {
    		e.printStackTrace();
    		result = "error";
    	}
    	dataMap.put("result", result);
    	
    	String resultStr = com.alibaba.fastjson.JSONObject.toJSONString(dataMap);
    	return jsonpCallback + "(" + resultStr + ")";
    }



  • 相关阅读:
    ThinkPHP 3.2 调用自定义函数库
    phpstorm 2017版代码提示功能开启解决方案
    phpstorm 2017激活
    JavaScript返回上一页和返回上一级页面并刷新
    PHP处理Ajax请求与Ajax跨域
    13个能快速开发android的经典项目
    分享6款优秀的 AR/VR 开源库
    Android-----购物车(包含侧滑删除,商品筛选,商品增加和减少,价格计算,店铺分类等)
    Android------视频播放器(包含全屏播放,快退,快进,腾讯新闻的列表播放等)
    吴恩达课后作业学习2-week1-2正则化
  • 原文地址:https://www.cnblogs.com/dulinan/p/12033060.html
Copyright © 2020-2023  润新知