-
-
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.
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 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... | |
}); |
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
/** | |
* 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; | |
} | |
} |
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 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