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 Concepts A Simple AJAX Example

Zakher Masri
Zakher Masri
4,415 Points

Two Ajax requests with one open() method?

So as I was following the instructions in the video, I wrote the following code:

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
      if (xhr.readyState === 4) {
        document.getElementById("ajax").innerHTML = xhr.responseText;
      }
    };
    xhr.open('GET', 'sidebar.html');
    xhr.send();

So from the code I can assume that the request will only get sidebar.html, which worked properly. But another XML request was shown in the console:

XHR finished loading: GET "chrome-extension://mgijmajocgfcbeboacabfgobmjgjcoja/content.min.css"

Which is obviously from a chrome extension. The questions I thought about (and haven't yet looked up) is about the open() method and whether we can request more than one object that can vary in source (a file or a url link). And if open() support multiple requests, is it better to separate each XML request in a different variable and called with it's own open() method or included in one variable that are called in one method?

2 Answers

I'm not sure what you are thinking of doing exactly but it sounds like you want to roll the plug-in's ajax request into yours to save http requests across the board. I'm not an expert at those kinda things but I don't think you're going to be able to mess with the Chrome extension. In the end though, that code really has nothing to do with code you are writing for a web page. My advice, ignore that extra AJAX call and press on.

AJAX by definition is asynchronous, so you can have multiple requests be sent out instead of waiting for each one to finish before sending the next.

The XMLHttpRequest.open() method however, is only for a single HTTP request.

There are ways of grouping AJAX requests together, including jQuery's Deferred Object.

Here's a good article with an example: Multiple Simultaneous Ajax Requests (with one callback) in jQuery