AJAX Flashcards

1
Q

What does XHR Stand For?

A

XMLHttpRequest object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is XHR?

A

The XMLHttpRequest object is a JS API that allows us to transfer data between a client and a server and update parts of a page without refreshing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What type of data is the standard to be used with XHR?

A

JSON - JavaScript Object Notation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Add an HTML link that will use XHR to getRepositories( )

A

<div>
<h3>Repositories</h3>
<a>Get Repositories</a>
</div>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Create a getRepositories function and initiate our XHR request.

A

function getRepositories() {
const req = new XMLHttpRequest();
req.addEventListener(‘load’, showRepositories);
req.open(‘GET’, ‘https://api.github.com/users/octocat/repos’);
req.send();
}

Here, we’re creating a new instance of an XMLHttpRequest. We call open with the HTTP verb we want, in this case GET, and the URI for the request.

Now that our request is set up and ready to go, we call send to make it happen.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Add an event listener with a callback function that will get called when the event fires.

A

function getRepositories() {
const req = new XMLHttpRequest();
req.addEventListener(‘load’, showRepositories);
req.open(‘GET’, ‘https://api.github.com/users/octocat/repos’);
req.send();
}

function showRepositories() {
  //this is set to the XMLHttpRequest object that fired the event
  console.log(this.responseText);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Create a getCommits REQUEST function that listens for a response

A

function getCommits(el) {
const name = el.dataset.repo;
const req = new XMLHttpRequest();
req.addEventListener(‘load’, showCommits);
req.open(‘GET’, ‘https://api.github.com/repos/octocat/’ + name + ‘/commits’);
req.send();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Create a showCommits function that shows the response

A
function showCommits() {
  const commits = JSON.parse(this.responseText);
  const commitsList = `<ul>${commits
    .map(
      commit =>
        '<li><strong>' +
        commit.author.login +
        '</strong> - ' +
        commit.commit.message +
        '</li>'
    )
    .join('')}</ul>`;
  document.getElementById('commits').innerHTML = commitsList;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Create a place in HTML to put the commits

A

<div>
<h3>Commits</h3>
<div></div>
</div>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does jQuery do?

A

Abstracts XHR into easier, higher-level \code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What Does AJAX stand for?

A

Asynchronous JavaScript and XML

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does AJAX do?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What’s the diff between XHR, AJAX & Jquery?

A

XMLHttpRequest is raw object for ajax request. It is lightest performance wise but it is not cross-browser compatible so using this you will loose cross-browser accessability for your Asynchronous request while $.ajax() is provided by jquery that adds cross-browser compatibility to asynchronous request

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Give an example of how we could use jQuery to perform an Ajax request.

A
var url =
    'https://api.github.com/repos/rails/rails/commits?sha=82885325e04d78fb7ec608a4670164d842d23078';

$.get(url).done(function(data) {
console.log(‘Done’);
console.log(data);
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to use jQuery,get()

A

https://api.jquery.com/jquery.get/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

AJAX relies on these technologies:

A
Things called Promises
Things called XMLHttpRequestObjects
A serialization format called JSON
asynchronous Input / Output
the event loop
17
Q

JS vs jQuery vs AJAX vs PHP

A

https://bsscommerce.com/blog/javascript-jquery-ajax-are-they-the-same-or-different/#:~:text=While%20JQuery%20is%20a%20library,used%20on%20a%20web%20page.&text=At%20the%20same%20time%2C%20jQuery,to%20program%20for%20the%20browser.