1、创建一个空的html文件test.html,引入jquery
2、调用ajax
$.ajax({
url: 'http://XXXXXXXXXXXXXX',
type: 'post',
success: function() {
console.log('成功');
},
error: function() {
console.log('失败');
}
});
3、url中给出的是项目的接口地址,直接访问是可以得到数据的
4、为何直接在浏览器打开test.html就会打印失败,并提示
XMLHttpRequest cannot load *******(接口地址). No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'file://' is therefore not allowed access.
5、这是跨域导致的错误吗?该如何解决?
补充:1、后端返回的数据格式是json
2、同事给的方案:在tomcat中配cors的filter,添加了相关jar包(cors-filter-2.5,java-property-utils-1.10),不起作用
在windows系统中,新建chrome浏览器的快捷方式,右键-目标,加入 --disable-web-security --user-data-dir
在mac系统中,终端执行:open -a "Google Chrome" --args --disable-web-security --user-data-dir