Cannot load Google images from my API using jQuery getJSON

My code is below:

$.getJSON('https://ajax.googleapis.com/ajax/services/search/images?q=Google&v=1.0', 
function(json) {
  alert(json);
})​

You can try this code here: http://jsbin.com/ofaru3/edit

Ajax is a mistake

imagesFailed to load a resource

How cna am i fixing this problem? Thank!

+3
source share
1 answer

You need &callback=?in the url to run JSONP, for example:

$.getJSON('https://ajax.googleapis.com/ajax/services/search/images?q=Google&v=1.0&callback=?', 
function(json) {
  alert(json);
});

You can check it out here . Without &callback?it, it tries to retrieve data from the remote domain using XmlHttpRequest (AJAX) and will not / be blocked due to the same origin policy . This is just the type of JSONP situation for.

$.getJSON() docs:

JSONP
URL- "callback =?" ( , API- ), JSONP. . jsonp $.ajax().

+6

Source: https://habr.com/ru/post/1774266/


All Articles