跨域访问解决方案

当你在服务器外部或另一个服务器中向当前服务器中发送部分请求时,如Ajax请求,会受到浏览器同源策略的限制,No 'Access-Control-Allow-Origin' header is present on the requested resource',解决方法如下:

@RequestMapping(value = "/query",method = RequestMethod.GET)
public Map<String,Object> queryRunoob(HttpServletResponse response){
    response.setHeader("Access-Control-Allow-Origin", "http://localhost:8080");
    response.setHeader("Access-Control-Allow-Methods", "GET,POST");
    Map<String,Object> modelMap = new HashMap<String,Object>();
    .
    .
    .
    return modelMap;
}

红色部分即为允许访问的ip和端口,也可以设置为*,即允许所有请求。

猜你喜欢

转载自www.cnblogs.com/YeHuan/p/12120505.html
今日推荐