Talking To Your Server Flashcards

1
Q

Name and describe the three states a promise object can have.

A

It can have one of three states: pending, fulfilled, and rejected. When it is first created and hasn’t resolved yet, it has a state of pending. If the promise is resolved successfully, its state changes to fulfilled. If the promise fails, its state changes to rejected. Once a promise’s state changes from pending, it is never changed again.

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

What’s a promise?

A

A promise is an object that represents the eventual results of an asynchronous operation.

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

Write a click event for #get-json to get data.json and display a success or error message.

A

$(‘#get-json’).click(function (event) {

$.getJSON(‘data.json’).then(function(data){

alert(data.message); }, function(err){

alert(‘ERROR:’ + JSON.stringify(err));

});

}0029;

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

What does the .getScript() method do?

A

The .getScript() method is different. It retrieves JavaScript from the server, parses, and then executes it. Like the other Ajax methods, it returns a promise, but in this case the success function is not passed any data.

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

Write a click event for #get-javascript to get script.js and display a success or error message.

A

$(‘#get-javascript’).click(function (event) {

$.getScript(‘script.js’).then(function () {

alert(“getScript() was successful.”);

}, function(err){

alert(‘ERROR:’ + JSON.stringify(err));

}

});

});

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

Upon click of #get, use the $.get() method to get data.json and display it.

A

$(‘#get’).click(function (event) {

$.get(“data.json”, function (data) {

showJsonMessage(data);

});

});

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

What are the four main HTTP verbs and what do they do?

A

get, post, put, and delete. Get is intended to retrieve one or more items from a database. Post is intended to create a new record in the database. Put is meant to update an existing record. And finally, delete removes a record from the database.

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

What are the 3 AJAX helper methods and what do they do?

A
  1. ) The $.param() method creates a serialized representation of an array or an object. The serialized values can be used in the URL query string when making an AJAX request.
  2. .serialize() encodes a set of form elements as a string for submission.
  3. ) .serializeArray() encodes a set of form elements as an array of JS objects, each with a name and a value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the 5 jQuery AJAX shorthand methods and what do they do?

A
  1. ) jQuery.get() - Load data from the server using a HTTP GET request.
  2. ) jQuery.getJSON() - Load JSON-encoded data from the server using a GET HTTP request.
  3. ) jQuery.getScript() - Load a JavaScript file from the server using a GET HTTP request, then execute it.
  4. ) jQuery.post() - Load data from the server using a HTTP POST request.
  5. ) .load() - Load data from the server and place the returned HTML into the matched element.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a ‘click’ event handler for “#jq-param” that

  1. ) includes an array with ‘first,last,email’ pairs stored as the var testData
  2. ) encodes and stores the array in a var myParam
  3. ) writes the output to #outputRegion
A

$(‘#jq-param’).click(function (event) {

var testData = [

{name: “first”, value: “Troy”},

{name: “last”, value: “Miles”},

{name: “twitter”, value: “@therockncoder”}

];

var myParam = $.param(testData);

$(‘#outputRegion’).text(myParam);

});

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

Write an event handler for the click function on ‘#get-get’ to use the full jQuery.ajax() method to GET ‘data1.json’ and display the data from ‘data1.json’.

A

$(‘#get-get’).click(function (event) {

$.ajax({

method: “GET”,url: “data1.json”,
success: function (data) {

showJsonMessage(data);

});

});

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

Write an event handler for the click function on #serializeArray to:

  1. ) convert the data of #myForm using serializeArray to var myParam
  2. ) convert myParam to string and output to #outputRegion.
A

$(‘#serializeArray’).click(function (event) {

var myParam = $(‘#myForm’).serializeArray(); $(‘#outputRegion’).text(JSON.stringify(myParam));

});

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

Write code to:

  1. ) store the document as a variable
  2. ) .show() a div, “dataTranser” when AJAX activity starts
  3. ) .hide() the div when AJAX activity stops
A

var $doc = $(document);

$doc.ajaxStart(function () {

$(‘#dataTransfer’).show(‘fast’);

});

$doc.ajaxStop(function () {

$(‘#dataTransfer’).hide(‘slow’);

});

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

What’s the difference between .ajaxSuccess() and .ajaxComplete();

A

The .ajaxComplete() handler is called whenever an Ajax request completes. This method is always called regardless of whether the request succeeded or failed. It is always called after the success or error event.

.ajaxSuccess() only shows if a request succeeds.

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