跨域报错解决方案Access to XMLHttpRequest at ‘http://xxx.com/xxx‘ from origin ‘null‘ has been blocked by
Posted 征途黯然.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了跨域报错解决方案Access to XMLHttpRequest at ‘http://xxx.com/xxx‘ from origin ‘null‘ has been blocked by相关的知识,希望对你有一定的参考价值。
错误背景描述:
在使用ajax调用api接口的时候:发生错误如下
Access to XMLHttpRequest at ‘http://xxxx.com/xxx’ from origin ‘null’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
发生错误的ajax代码如下:
$.ajax(
url: 'http://xxx.com/xxxxx',
type: 'POST',
dataType: 'json',
success:function(data)
console.log(data)
);
错误发生原因:
这是一个资源跨域问题。
解决方法:
很简单。把dataType的json改为jsonp。
$.ajax(
url: 'http://xxx.com/xxxxx',
type: 'POST',
dataType: 'jsonp',//这里修改成jsonp
success:function(data)
console.log(data)
);
以上是关于跨域报错解决方案Access to XMLHttpRequest at ‘http://xxx.com/xxx‘ from origin ‘null‘ has been blocked by的主要内容,如果未能解决你的问题,请参考以下文章