Spring RestController的跨域ajax请求问题 [英] Issue with cross domain ajax request with spring restcontroller

查看:414
本文介绍了Spring RestController的跨域ajax请求问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临ajax跨域请求的问题.

I am facing issue with cross domain request with ajax.

以下是我的弹簧控制器的代码:

Following is my spring controller's code:

@RestController
@RequestMapping(value = "/help")
public class MYController {

        @Autowired
        private MyService myService;

        private static final int SUCCESS = 1;
        private static final int FAIL    = 0;

        @RequestMapping(value = "save", method = RequestMethod.POST,produces={"application/json"})
        public int save(@RequestBody Item item,HttpServletRequest request,HttpServletResponse response) {

                response.addHeader("Access-Control-Allow-Origin","*");  
                response.addHeader("Access-Control-Allow-Methods","GET, PUT, POST, DELETE, OPTIONS");  
                response.addHeader("Access-Control-Allow-Headers","Content-Type, Content-Range, Content-Disposition, Content-Description");  

                try {
                        myService.save(item);
                        return SUCCESS;
                } catch (Exception ex) {
                        ex.printStackTrace();
                        return FAIL; 
                }
        }  
}

以上代码是我的REST服务的一部分,它正在其他Tomcat上运行.

above code is a part of my rest service and it is running on other tomcat.

以下是我的脚本代码,这是我的客户端代码.

Following is my script code , which is my client code.

$.ajax({
               type: "POST",
               url:"http://10.10.13.215:9092/helpProject/help/save",
               crossDomain: true,
               dataType: "JSON",
               data:JSON.stringify(item), //item is my pojo here
               headers : {Accept : "applicationjson","Access-Control-Allow-Origin" : "*"},
                   success: function(msg) {
                         alert(msg);
            },
             error: function (e) { 
                   alert("errorrrrrr");
             }
            });

我在浏览器控制台中遇到以下错误:

I am getting following error in browser's console:

XMLHttpRequest cannot load http://10.10.13.215:9092/helpProject/help/save",
     No 'Access-Control-Allow-Origin' header is present on the requested resource. 
     Origin 'http://localhost:8082' is therefore not allowed access.

请帮助解决..谢谢

推荐答案

尝试添加此过滤器.

@Component
public class CorsFilter implements Filter {


@Override
public void init(FilterConfig filterConfig) throws ServletException {
    // TODO Auto-generated method stub

}


@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
    HttpServletResponse res = (HttpServletResponse) response;
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Methods",
            "POST, GET, OPTIONS, DELETE");
    res.setHeader("Access-Control-Max-Age", "3600");
    res.setHeader("Access-Control-Allow-Headers", "x-requested-with");
    chain.doFilter(request, res);
}


@Override
public void destroy() {
    // TODO Auto-generated method stub

}

}

这篇关于Spring RestController的跨域ajax请求问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆