Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active September 27, 2016 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/5766eb39c10e441853db48d45028ada2 to your computer and use it in GitHub Desktop.
Save tommcfarlin/5766eb39c10e441853db48d45028ada2 to your computer and use it in GitHub Desktop.
[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