This is a thing by Kyle Hill
He is a software engineer at JIBE
(Sorry if you knew that already)
// Creates a new instance of the JS-native XMLHttpRequest class
var request = new XMLHttpRequest();
// Creates a listener for the "load" event
// Executes this function when data loads
request.onload = function() {
console.log(this.responseText);
};
/* Prepares to send a request:
* First parameter: the HTTP verb used with the server
* Second parameter: the relative path to where we're getting data
* Third parameter: Should we do this asychronously? (Yes.)
*/
request.open("get", "/api/cats", true);
// Fire zee missles
request.send();
// Creates a new instance of the JS-native XMLHttpRequest class
var request = new XMLHttpRequest();
// Creates a listener for the "load" event
// Executes this function when data loads
request.onload = function() {
console.log(this.responseText);
};
/* Prepares to send a request:
* First parameter: the HTTP verb used with the server
* Second parameter: the relative path to where we're getting data
* Third parameter: Should we do this asychronously? (Yes.)
*/
request.open("get", "/api/cats", true);
// Fire zee missles
request.send();
Fuck it, just use jQuery.
$.ajax({
// Relative path to where we're getting data
url: "/api/cats",
// Executes this function when data loads
success: function(data) {
console.log(data);
}
});
$.ajax({
// Relative path to where we're getting data
url: "/api/cats",
// JS object, contents passed to the server with the request
data: { color: "brown" },
method: "GET",
success: function(data) {
console.log(data);
},
// Executes this function when something breaks
error: function() {
doSomethingElseInstead();
},
// Executes this function after success or failure
complete: function() {
ayoImDoneWithMyTransaction();
}
});
With great power comes