I have an ajax function outputs results of +1 and -1, in javascript, I want to get the +1 response to use in colorbox.
This is the ajax function:
function my_ajax_obj_lightbox() {
global $post;
if ( !empty( $_POST['obj_id'] ) ) {
$obj = new My_Obj( $_POST['obj_id'] );
if ( $obj instanceof My_Obj ) {
my_objs_ajax_response_string( 1, $obj->html() );
}
}
my_objs_ajax_response_string( -1, 'Invalid request' );
}
add_filter( 'wp_ajax_obj_lightbox', 'my_ajax_obj_lightbox' );
Now , in javascript, I want to say something like this:
var response = ......;
if ( response[0] >= 1 ) {
j.fn.colorbox({
html: response[1],
maxWidth: '90%',
maxHeight: '90%'
});
}
How to define the var response and get ajax response as value ?
It sounds like you just need help setting up the AJAX request, and parsing the response into a JavaScript object…
Let’s say you have a PHP file that returns a response in JSON format:
To retrieve this using AJAX, you can do something like this:
If you’re using jQuery, here’s the equivalent code:
Documentation for jQuery post:
http://api.jquery.com/jQuery.post/