Skip to content

Instantly share code, notes, and snippets.

@garyc40
Created October 3, 2012 15:39
Show Gist options
  • Save garyc40/3827632 to your computer and use it in GitHub Desktop.
Save garyc40/3827632 to your computer and use it in GitHub Desktop.
WordPress AJAX JSON
// embed the javascript file that makes the AJAX request
// don't forget to specify 'json2' as the dependency
wp_enqueue_script( 'my-ajax-request', plugin_dir_url( __FILE__ ) . 'js/ajax.js', array( 'jquery', 'json2' ) );
wp_enqueue_script( 'json2' );
// don't forget to specify dependency
wp_enqueue_script( 'myjQuery', plugin_dir_url( __FILE__ ) . 'js/jquery.1.4.2.js', array( 'json2' ) );
wp_enqueue_script( 'json2' );
jQuery.post( MyAjax.ajaxurl, { action : 'myajax-submit' },
function( jsonString ) {
// use JSON parser on the response string instead of
// specifying the response type as json
var object = JSON.parse(jsonString);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment