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. 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'
).
If your proxy server or container adds the following header when serving the .js file, it will force some browsers such as Chrome to perform strict checking of MIME types:
X-Content-Type-Options: nosniff
Remove this header to prevent Chrome performing the MIME check.
FYI, I've got the same error from Chrome console. I thought my AJAX function causing it, but I uncommented my minified script from /javascripts/ajax-vanilla.min.js
to /javascripts/ajax-vanilla.js
. But in reality the source file was at /javascripts/src/ajax-vanilla.js
. So in Chrome you getting bad MIME type error even if the file cannot be found. In this case, the error message is described as text/plain
bad MIME type.
上一篇: 拒绝执行脚本