-
-
Save tommcfarlin/2dbe93fe47a96657e2ce7e4da0313bab to your computer and use it in GitHub Desktop.
[JavaScript] Properly Working with Ajax Requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _processArticle = function() { | |
var request = | |
$.post(acme.ajax_url, { | |
action: 'acme_get_data', | |
url: sUrl | |
security: acme.security | |
}, function(response) { | |
// ... | |
}) | |
.done(function(response) { | |
// ... | |
}) | |
.fail(function(xhr, status, error) { | |
// ... | |
}) | |
.always(function() { | |
// .. | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _processArticle = function() { | |
var request = | |
$.post(acme.ajax_url, { | |
action: 'acme_get_data', | |
url: sUrl | |
security: acme.security | |
}, function(response) { | |
// Abort all request operations. | |
if (false === response.success) { | |
request.abort(); | |
} | |
// Handle the successful response by modifying the DOM. | |
}) | |
.done(function(response) { | |
// ... | |
}) | |
.fail(function(xhr, status, error) { | |
// ... | |
}) | |
.always(function() { | |
// ... | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _processArticle = function() { | |
var request = | |
$.post(acme.ajax_url, { | |
action: 'acme_get_data', | |
url: sUrl | |
security: acme.security | |
}, function(response) { | |
// Abort all request operations. | |
if (false === response.success) { | |
request.abort(); | |
} | |
// Handle the successful response by modifying the DOM like flashing the first image. | |
$('img:first').fadeTo('fast', 0.5, function() { | |
$(this).fadeTo('fast', 1); | |
}); | |
}) | |
.done(function(response) { | |
// ... | |
}) | |
.fail(function(xhr, status, error) { | |
console.log('There was a problem with contacting the API.'); | |
console.log('The status was:'); | |
console.log(status); | |
console.log('The error was:') | |
cosole.log(error); | |
}) | |
.always(function() { | |
// ... | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _processArticle = function() { | |
var request = | |
$.post(acme.ajax_url, { | |
action: 'acme_get_data', | |
url: sUrl | |
security: acme.security | |
}, function(response) { | |
// Abort all request operations. | |
if (false === response.success) { | |
request.abort(); | |
} | |
// Handle the successful response by modifying the DOM like flashing the first image. | |
$('img:first').fadeTo('fast', 0.5, function() { | |
$(this).fadeTo('fast', 1); | |
}); | |
}) | |
.done(function(response) { | |
// ... | |
}) | |
.fail(function(xhr, status, error) { | |
console.log('There was a problem with contacting the API.'); | |
console.log('The status was:'); | |
console.log(status); | |
console.log('The error was:') | |
console.log(error); | |
}) | |
.always(function() { | |
// Regardless of what happens, this is where you can provide the user feedback. | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _processArticle = function() { | |
var request = | |
$.post(acme.ajax_url, { | |
action: 'acme_get_data', | |
url: sUrl | |
security: acme.security | |
}, function(response) { | |
// Abort all request operations. | |
if (false === response.success) { | |
request.abort(); | |
} | |
// Handle the successful response by modifying the DOM. | |
}) | |
.done(function(response) { | |
// Display any additional changes you may want to make based on the respnse. | |
}) | |
.fail(function(xhr, status, error) { | |
console.log('There was a problem with contacting the API.'); | |
console.log('The status was:'); | |
console.log(status); | |
console.log('The error was:') | |
console.log(error); | |
}) | |
.always(function() { | |
// Regardless of what happens, this is where you can provide the user feedback. | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment