Ajax请求跨域问题

  1. 遇到ajax请求跨域问题,解决方式,改dataType为jsonp
$.ajax({
                type: "GET",
                url: "http://localhost:8080/data/info/page.jspx?node=domestic",
                dataType: "jsonp",
                success: function (data) {}
            });
  1. json和jsonp返回数据格式
    json格式
{
    "message":"获取成功",
    "state":"1",
    "result":{"name":"工作组1","id":1,"description":"11"}
}

jsonp格式

callback({
    "message":"获取成功",
    "state":"1",
    "result":{"name":"工作组1","id":1,"description":"11"}
})
  1. 后台接受参数,并修改返回数据格式 (return callBack + "(" + rsMsg.toJSONString() + ")";)
    @ResponseBody
    @RequestMapping(value = "/page.jspx", produces = "application/json;charset=utf-8")
    public String getInfoPage(HttpServletRequest request, HttpServletResponse response, Model modelMap) {
        JSONObject rsMsg = new JSONObject();
        String callBack = request.getParameter("callback");
        ...
        ...
        rsMsg.put("status", "200");
        rsMsg.put("message", "success");
        return callBack + "(" + rsMsg.toJSONString() + ")";
    }

你可能感兴趣的:(Ajax请求跨域问题)