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

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

问题描述

我试图通过AJAX以JSON的身份访问脚本,该脚本可以在Safari和其他浏览器上正常工作,但不幸的是无法在Chrome中执行。它出现以下错误:


拒绝从'*'执行脚本,因为它的MIME类型('application / json')不是

以下是请求:

  $。ajax({
url:http://some_url/test.json?callback =?,
类型:GET,
dataType:'json',
cache:true,
成功:函数(数据,状态,错误){
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


$ b (当你这样做时,不要告诉jQuery你期待JSON,因为这是矛盾的: dataType:'jsonp')。


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.

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?

解决方案

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 is not JSON. It is a JavaScript program.

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

(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天全站免登陆