Jquery的jsonp方式的ajax提交解决跨域的问题

jquery请求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var action = form.attr("action");
$.ajax({
  type:"get",
  data: {
      mobile : $('#mobile'+id).val()
  },
  url:action,
  dataType:"jsonp",
  jsonp:"callback",
  success:function(data){
      var res = eval(data);
      if(res.resultObj.result){
          alert("发送成功");
      }
  }
});

服务器端:

1
2
3
4
5
6
7
8
9
10
11
response.setContentType("text/html; charset=utf-8");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
//业务逻辑
String callback = request.getParameter("callback");
result = callback+"("+resultJson.toString()+")";
out.print(result);
out.flush();
out.close();