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?
This question is related to
javascript
jquery
ajax
json
google-chrome
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
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.
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
});
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.
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.
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.
Source: Stackoverflow.com