Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin

tommcfarlin/00-ajax-request.js Secret

Last active Sep 27, 2016
Embed
What would you like to do?
[WordPress] Approaching client-side Ajax functionality across projects will provide you with a formula that works for many WordPress-based projects.
var data = {
action: 'process_file_information',
file_number: 10
};
$.post( ajaxurl, data, function( response ) {
/* Typically, check the response before trying to parse it.
* I'm cutting it down for brevity.
*/
response = $.parseJSON( response );
// More to come...
});
/**
* Parses the JSON object to determine if the incoming response
* is valid.
*
* @param JSON json_response The response from the server.
*/
var parse_response = function( json_response ) {
/* Assume that our response looks something like this:
* {"MESSAGE":"COMPLETE","FILE_NUMBER":"10"}
*/
switch( json_response.MESSAGE ) {
case "COMPLETE":
// Handle the 'Complete' functionality here.
break;
case "INCOMPLETE":
// Handle the 'Incomplete' functionality here.
break;
default:
// You have message that's not part of the protocol.
break;
}
}
var data = {
action: 'process_file_information',
file_number: 10
};
$.post( ajaxurl, data, function( response ) {
/* Typically, check the response before trying to parse it.
* I'm cutting it down for brevity.
*/
response = $.parseJSON( response );
parse_response( response );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment