Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,236 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.