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 Programming AJAX Programming AJAX Solution

I've concluded the AJAX basic course but can't identify the similarity with the following pattern. Could you help?

The AJAX course was excellent for knowledge not only in AJAX, but also related technologies. The structure of the code in the course was:

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

            function sendAJAX() {
             xhr.send();
             document.getElementById(load).style.display = none;
            }
</script>

But in the projects I'm working, I keep encountering AJAX in the form:

<script>
   $.ajax({
      type: "POST",
      contentType: "application/json",
      url: "/api/search",
      data: JSON.stringify(search),
      dataType: 'json',
      cache: false,
      success: function (data) {
         //some code
      }
   }); 
</script>

My point is not to understand what the code above does, but to know the difference between this structure and the one learned in the course. What is it and what should I look for to learn it?

Thank you

Murilo

1 Answer

Timothy Sawyer
Timothy Sawyer
31,052 Points

The top code snippet is using vanilla/plain JavaScript and the lower one is using jQuery syntax.

treehouse AJAX course

w3 Schools