Ajax跨域问题在开发过程中会经常遇到,我们可以通过将$.ajax()的参数'dataType'设置成'jsonp'或者设置request对象的请求头来解决:
$.ajax({ url:url, method: "POST", dataType:'json', data:{ id:id }, success: function (result) { }});java代码如下:@RequestMapping("/queryContentCount.do")@ResponseBodypublic void queryContentCount(HttpServletRequest req, HttpServletResponse resp) throws JSONException, IOException{ req.getHeader("access-Control-Allow-Origin:*");//此处设置 String id =req.getParameter("id"); if(StringUtils.isNotBlank(id)){ resp.setHeader("Access-Control-Allow-Origin", "*");//此处设置 resp.setContentType("text/plain;charset=UTF-8"); PRintWriter out = resp.getWriter(); out.write(id); } }拿走不谢;
新闻热点
疑难解答