Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

javascript - 由于MIME类型错误,Chrome拒绝执行AJAX脚本(Chrome refuses to execute an AJAX script due to wrong MIME type)

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.

(我正在尝试通过AJAX以JSON格式访问脚本,该脚本在Safari和其他浏览器上可以正常运行,但不幸的是无法在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.

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

Here's the request:

(这是请求:)

$.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?

(有人对此有解决方法吗?)

  ask by Paul Nelligan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(通过添加一个回调参数,您告诉jQuery您想使用脚本元素而不是XMLHttpRequest来请求JSONP。)

JSONP is not JSON.

(JSONP不是JSON。)

It is a JavaScript program.

(这是一个JavaScript程序。)

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

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

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

((尽管如此,请不要告诉jQuery您期望使用JSON,因为这是矛盾的: dataType: 'jsonp' )。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...