问题描述
我试图访问一个脚本JSON通过AJAX,它正常工作的Safari和其他浏览器,但遗憾的是不能在Chrome浏览器中执行。它的到来,出现以下错误:
I'm trying to access a script as JSON via AJAX, which works fine on safari and other browsers but unfortunately will not execute in Chrome. It's coming with the following error:
Refused to execute script from '*' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled.
下面的请求:
$.ajax({
url: "http://some_url/test.json?callback=?",
type: "GET",
dataType: 'json',
cache: true,
success: function (data, status, error) {
console.log('success', data);
},
error: function (data, status, error) {
console.log('error', data, status, error);
}
});
有没有人有一个解决方法吗?
Does anyone have a workaround for this?
感谢
保
推荐答案
通过添加您要使用使用XMLHtt $ P脚本元素,而不是为JSON的请求作出了JSONP请求的回调参数,你告诉jQuery的$ pquest。
By adding a callback argument, you are telling jQuery that you want to make a request for JSONP using a script element instead of a request for JSON using XMLHttpRequest.
JSONP不是JSON。这是一个JavaScript程序。
JSONP is not JSON. It is a JavaScript program.
更改您的服务器,以便其输出正确的MIME类型JSONP是应用程序/ JavaScript的
。
Change your server so it outputs the right MIME type for JSONP which is application/javascript
.
(当你在这,停止告诉jQuery的是你期待的JSON因为这是矛盾的:数据类型:JSONP
)。
(While you are at it, stop telling jQuery that you are expecting JSON as that is contradictory: dataType: 'jsonp'
).
这篇关于拒绝从执行脚本'*',因为它的MIME类型(“应用/ JSON')不是可执行文件,并严格MIME类型检查被启用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!