William
William

Reputation: 4588

Why does iTunes API access work via JQuery but not XMLHttpRequest Object?

I can access the itunes API using JQuery. The following works perfect without a server.

$(function(){

    var apiURL = "https://itunes.apple.com/search?term=funk&media=music&callback=?";

    $.getJSON(apiURL,function(data){
        console.log(data);
    });
});

I want to access it using the regular XMLHttpRequest object. I am curious how to make it work.

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://itunes.apple.com/search term=funk&media=music&callback=?');
xhr.send();

Upvotes: 0

Views: 155

Answers (1)

Dylan Watt
Dylan Watt

Reputation: 3387

Since your URL has a callback in the query string, getJSON uses jsonp. http://api.jquery.com/jquery.getjson/#jsonp

This loads the data in via a script tag rather than via XHR. JSONP is the only way to get that particular resource to load across domains, since Access-Control-Allow-Origin isn't set.

Upvotes: 1

Related Questions