由于 MIME 类型错误,Chrome 拒绝执行 AJAX 脚本 [英] Chrome refuses to execute an AJAX script due to wrong MIME type

查看:69
本文介绍了由于 MIME 类型错误,Chrome 拒绝执行 AJAX 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 AJAX 以 JSON 形式访问脚本,该脚本在 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:

拒绝从 '*' 执行脚本,因为其 MIME 类型('application/json')不可执行,并且启用了严格的 MIME 类型检查.

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.

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.

更改您的服务器,使其为 JSONP 输出正确的 MIME 类型,即 application/javascript.

Change your server so it outputs the right MIME type for JSONP which is application/javascript.

(当你在做的时候,不要告诉 jQuery 你期待 JSON,因为这是矛盾的:dataType: 'jsonp').

(While you are at it, stop telling jQuery that you are expecting JSON as that is contradictory: dataType: 'jsonp').

这篇关于由于 MIME 类型错误,Chrome 拒绝执行 AJAX 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆