Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Spencer Christensen
PLUS
Spencer Christensen
Courses Plus Student 8,180 Points

Ajax Console Error: Refused to execute script because its MIME type ('application/json') is not executable

Hello Treehouse world!

I am SO happy that an AJAX course was created. THANK YOU!

I have a question as I am now knee deep in 3rd party APIs. I am receiving the following error in my console when I load the page:

Refused to execute script from '?jsoncallb…###&contenttype=application%2Fjson&_=###' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled.

I removed my company for privacy.

Here is my code:

$(document).ready(function () {
  var url = 'https://mycompany.zendesk.com/api/v2/satisfaction_ratings.json?'
  var zDdata = {
  };
  var displayResults = function(data) {
    console.log(data);
  };
  $.getJSON(url, zDdata, displayResults);
});

I have tried passing quite a bit into my data object but, alas, this has had no effect.

I can confirm that the API I am using 1. exposes content via AJAX 2. passes JSONP

At the very least can somebody elaborate on how MIME types relate to AJAX and execution?

Any assistance is very, very appreciated. Thanks in advance.

4 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Without knowing the exact URL it will be hard to debug this,but it sounds like it's a server issue -- the mime type isn't being set correctly. Here are a couple of things I dug up in a search: -- http://stackoverflow.com/questions/24528211/refused-to-execute-script-from-because-its-mime-type-application-json-is -- http://webmasters.stackexchange.com/questions/50006/chrome-refused-to-execute-this-javascript-file

Try removing the ?jsoncallback=? part of the URL and see what happens.

Spencer Christensen
PLUS
Spencer Christensen
Courses Plus Student 8,180 Points

Wow, thanks for replying so quickly Dave! How cool!

Thank you for sharing those articles. I likewise scoured stackoverflow et al to no avail. That being said, two new things:

Firstly, I have decided to reveal additional info on what I am doing. I am attempting to retrieve my company's satisfaction ratings from Zendesk using this API: http://developer.zendesk.com/documentation/rest_api/satisfaction_ratings.html

Relevant Docs: http://developer.zendesk.com/documentation/rest_api/introduction.html & http://developer.zendesk.com/documentation/apps/reference/request.html

Secondly, I am no longer able to replicate the original error for MIME type. Instead, I am now encountering one of the two errors:

XMLHttpRequest cannot load https://thismomentcs.zendesk.com/api/v2/satisfaction_ratings.json?. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

OR

GET https://thismomentcs.zendesk.com/api/v2/satisfaction_ratings.json?jsoncallb…ow-Credentials=true&cors=true&dataType=jsonp&proxy_v2=true&_=1404402959989 403 (Forbidden)

This is when keeping the callback function and trying a include a great variety of headers. Here is a sampling of this code:

$(document).ready(function () { var url = 'https://mycompany.zendesk.com/api/v2/satisfaction_ratings.json?jsoncallback=?' var zdData = { 'Accept': 'application/javascript', 'contentType': 'application/javascript', 'Access-Control-Allow-Credentials': 'true', 'cors':'true', 'dataType': 'jsonp', 'proxy_v2': 'true' }; var displayRatings = function(data) { console.log(data); }; $.getJSON(url, zdData, displayRatings); });

Does anybody have any suggestions/thoughts?

Spencer Christensen
PLUS
Spencer Christensen
Courses Plus Student 8,180 Points

Got ahold of Zendesk support and, wouldn't you know it, they do not support JSONP on their v2 API (they did on their v1 API). Well then, I feel silly.

Off to play with CORS and OAUTH!

Thanks again!

Hey Spencer Christensen and Dave McFarland I know this post is a year old but I am having this exact same issue and cannot figure out a work around. I am trying to call the indeed.com api. Ive gotten this error when I try method: 'JSONP'

Refused to execute script from '?jsoncallb…###&contenttype=application%2Fjson&_=###' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled.

and this error when I do a method:'GET'

I know I need to play with CORS but cant quite figure out how to implement it into my exiting code. Let me know if you remember how you did it! My code is below:

controllers.controller('JobsCtrl', function($scope, $http){
  $http({
    method: 'JSONP',
    url: 'http://api.indeed.com/ads/apisearch?publisher=3997996753290215&q=java&l=austin%2C+tx&sort=&radius=&st=&jt=&start=&limit=&fromage=&filter=&latlong=1&co=us&chnl=&userip=1.2.3.4&useragent=Mozilla/%2F4.0%28Firefox%29&v=2',

  }).then(function (data) {

       console.log(data);

     });



});