ajax跨域問題的解決辦法

$.ajax({    
       url: "http://localhost:8080/wechart/qryOrderInfoListByWeChart",    
       type: "post",    
       dataType: 'json',
       data: data,
       error: function (XMLHttpReuqest, textStautus, errothrown) {    
           console.log(XMLHttpRequest.status);     
       },    
       success: function (json) {  
           alert(json);  
       }    
   });  

瀏覽器調試一直報 XMLHttpRequest cannot load http://www.server.com/server.php. No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://www.client.com' is therefore not allowed access.
搜了下說是跨域的問題,網上找解決方法,加crossDomain:true,  type改成get啦、甚至監控瀏覽器類型和版本去修改ajax方法,試了十幾種方法,都不行!最後問了一個大牛同事,解決方法記錄一下,供碼農們參考:
修改ajax請求爲jsonp

$.ajax({
          url: "http://localhost:8080/wechart/qryOrderInfoListByWeChart?openId=1123&pageSize=5&pageNum=1",
          type: "POST",
          dataType: "JSONP",
          jsonp: "jsonpcallback",
          jsonpCallback: "qryCallback",
          success:function(msg){
          //alert(msg.resultMsg);
          },
          error: function (msg) {
              alert("查詢安裝工單失敗!");
          }
      });
function qryCallback(result) {
       console.log(result);
}
後臺代碼回調定義的callback方法:
@ResponseBody
@RequestMapping(value="/qryOrderInfoListByWeChart")
public void qryOrderInfoListByWeChart(HttpServletRequest request, HttpServletResponse response) throws Exception{
Map<String,Object> resultMap = new HashMap<String,Object>();
List<WorkOrderInstall> pageList = new ArrayList<WorkOrderInstall>();
Map<String,Object> param = new HashMap<String,Object>();
int pageSize=Integer.parseInt(request.getParameter("pageSize"));
int pageNum=Integer.parseInt(request.getParameter("pageNum"));
param.put("openId", request.getParameter("openId"));
param.put("pageNum", pageNum);
param.put("pageSize", pageSize);
param.put("pageStart", (pageNum-1)*pageSize);
pageList = workOrderService.qryOrderInfoByOpenId(param);
resultMap.put("resultType", "N");
resultMap.put("resultMsg", "success");
resultMap.put("pageList", pageList);
JSONObject jsonObject = JSONObject.fromObject(resultMap);
String jsonp=request.getParameter("jsonpcallback");  
PrintWriter pw = response.getWriter(); 
pw.print(jsonp+"("+jsonObject.toString()+")");
pw.flush(); 
pw.close(); 

}

或者這樣
@ResponseBody
@RequestMapping(value="/qryOrderInfoListByWeChart")
public String qryOrderInfoListByWeChart(String openId,int pageSize,int pageNum) throws Exception{
Map<String,Object> resultMap = new HashMap<String,Object>();
List<WorkOrderInstall> pageList = new ArrayList<WorkOrderInstall>();
Map<String,Object> param = new HashMap<String,Object>();
param.put("openId", openId);
param.put("pageNum", pageNum);
param.put("pageSize", pageSize);
param.put("pageStart", (pageNum-1)*pageSize);
pageList = workOrderService.qryOrderInfoByOpenId(param);
resultMap.put("resultType", "N");
resultMap.put("resultMsg", "success");
resultMap.put("pageList", pageList);
JSONObject jsonObject = JSONObject.fromObject(resultMap);
return "qryCallback("+jsonObject.toString()+")";

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章