我正在尝试通过 AJAX 以 JSON 格式访问脚本,该脚本在 Safari 和其他浏览器上运行良好,但不幸的是无法在 Chrome 中执行。它带有以下错误:
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);
}
});
有人有解决办法吗?
最佳答案
通过添加回调参数,您告诉 jQuery 您希望使用脚本元素发出 JSONP 请求,而不是使用 XMLHttpRequest 请求 JSON。
JSONP 不是 JSON。它是一个 JavaScript 程序。
更改您的服务器,使其为 JSONP 输出正确的 MIME 类型,即 application/javascript
。
(当你这样做时,不要告诉 jQuery 你期待 JSON,因为这是矛盾的:dataType: 'jsonp'
)。
关于javascript - 由于 MIME 类型错误,Chrome 拒绝执行 AJAX 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24528211/