Samir K Samir K - 1 year ago 306
Ajax Question

javascript: MIME type ('text/html') is not executable, and strict MIME type checking is enabled

I am trying to make a simple get authorization request to the Spotify Api. If i make a GET HTTPRequest then i get a cross-domain error. Hence I making a JSONP request using a callback, but this leads to a MIME type error as shown above, whose workaround i have found is to again make an HTTPRequest JSON request to match the MIME type. Quite a deadlock I am in here ! Please help! Thanks

This is my js code block:

var script = document.createElement('script');
script.src = '';

function mySpotify(){

Answer Source

Your problem seems to be that you’re using a <script> tag to load something that’s an HTML page. This is my suggested solution:

  1. When the user needs to authenticate, redirect them:

    location.href = "" + 
        "?client_id=" + CLIENT_ID +
        "&response_type=token" + 
        "&redirect_uri=" + encodeURIComponent(THE_URI_TO_REDIRECT_TO) + 
        "&state=" + STATE + // optional
        "&scope=" + SCOPES.join(" ") + // optional

    Note that if you’re going to redirect on page load, use location.replace(...) instead of location.href = .... This way, the user will not have the immediately-redirecting page in their back button history.

  2. Then, at the URL in THE_URI_TO_REDIRECT_TO, parse the hash:
    Spotify makes a hash like this: #access_token=...&expiry=.... location.hash returns that hash, including the leading #. First, we set up our object that will hold the options:

    var hash = {};

    then, we remove the #:

    var h = location.hash.slice(1)

    … and split on the &s.

    h = h.split('&')

    Next, we iterate over all of the pairs (forEach) and put the two pieces in the hash object (i. e. hash['access_token'] = '...';

    h.forEach(function(pair) {
        pair = pair.split('=');
        hash[pair.shift()] = pair.join('=');
  3. After that, you can read the data.

    if (hash.error) {
    } else {
        var token = hash.access_token;
        hash.token_type === "Bearer";
        var expiry = new Date();
        expiry.setSeconds(expiry.getSeconds() + (+hash.expires_in));
    var state = hash.state; // optional
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download