JavaScript AJAX Basics (retiring) AJAX and APIs Making the AJAX Request

Leonardo Motta
Leonardo Motta
11,284 Points

Making this request with vanilla js

How could I do the jQuery request with vanilla js?

I have this so far:

// Start http request
    const xhr = new XMLHttpRequest();
    const url = "https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";

    xhr.onload = function() {
      if (xhr.status === 200) {
        let returnValue = JSON.parse(xhr.responseText);
        console.log(returnValue);
      } else {
        console.log(this.status);
      }
    };
    xhr.open("GET", url);
    xhr.send();

1 Answer

Steven Parker
Steven Parker
177,727 Points

It looks like you have the right idea, but you're trying to access a resource that isn't providing the right information to pass the CORS security requirements of the browser. You may be able to invoke your browser with special options to avoid this issue. For example, for Chrome, you could start it with this command line:

chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security

Besides a much more compact syntax, another advantage of jQuery for AJAX is that it internally resolves many CORS issues without special browser settings.