synchronous jquery json request
-
11-09-2019 - |
Question
I need to make a synchronous request to the last.fm API, but when I use GET and json together, the request becomes asynchronous.
my code:
$.ajax({
async: false,
dataType: "json",
url: "http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist="+artist+"&api_key="+apiKey+"&format=json&callback=?",
success: function(html){
imgURL = html.images.image[1].sizes.size[0]["#text"];
}
});
If I remove dataType: "json" or use POST, it's synchronous again, but I'm dependant on using both json and GET.
Any ideas?
Solution
Ah, this is because you're trying to do cross-domain requests, and cross-domain requests rely on a dynamic script tag, which can never be synchronous, must use datatype json and the GET method.
If you do a POST or remove the datatype, you will get an access error because of same-origin policy. It will return immediately, but as a failure.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow