Reputation: 2005
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?
Upvotes: 146
Views: 337147
Reputation: 478
if application is hosted on IIS, make sure Static Content is installed. Control Panel > Programs > Turn Windows features on or off > Internet Information Services > World Wide Web Services > Common HTTP Features > Static Content.
I faced this problem when trying to run an existing application on a new IIS 10.0 installation
Upvotes: 0
Reputation: 19186
For the record and Google search users, If you are a .NET Core developer, you should set the content-types manually, because their default value is null or empty:
var provider = new FileExtensionContentTypeProvider();
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = provider
});
Upvotes: 2
Reputation: 2339
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.
Upvotes: 11
Reputation: 11464
I encountered this error using IIS 7.0 with a custom 404 error page, although I suspect this will happen with any 404 page. The server returned an html 404 response with a text/html mime type which could not (rightly) be executed.
Upvotes: 1
Reputation: 1382
In my case, I use
$.getJSON(url, function(json) { ... });
to make the request (to Flickr's API), and I got the same MIME error. Like the answer above suggested, adding the following code:
$.ajaxSetup({ dataType: "jsonp" });
Fixed the issue and I no longer see the MIME type error in Chrome's console.
Upvotes: 0
Reputation: 3065
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.
Upvotes: 59
Reputation: 944402
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'
).
Upvotes: 72