Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
218,635 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.