Loading

使用CROS解决跨域问题

使用jq的话,设置ajax这两个属性即可。

第一个为使用crossDomain,第二个属性使其可以传输cookie

$.ajaxSetup({
    crossDomain: true,
    xhrFields: {
        withCredentials: true
    }
});

但这种方式IE9并不支持,这时候强大的jq插件库的作用就显示出来了

插件地址:https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest

同时后台还需要进行设置,spring中直接在Controller上加上@CrossOrigin注解即可。

如果失效,请为Controller设置具体的method

@CrossOrigin
@RequestMapping(value = "/Controller", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)

如果不用spring需自行设置下面三个头信息

Access-Control-Allow-Origin
Access-Control-Allow-Credentials
Access-Control-Expose-Headers
posted @ 2017-06-15 22:43  hackyo  阅读(3739)  评论(0编辑  收藏  举报