javascript - ajax如何跨域?
本文介绍了javascript - ajax如何跨域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
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
这篇关于javascript - ajax如何跨域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文