PHP end of process looks like following:
$output = array(
'inputUrl' => 'some_value',
'title' => 'some_value',
'slug' => 'some_value',
);
echo json_encode( $output );
I get response like:
{"inputUrl":"some_value","title":"some_value","slug":"some_value"}0
AJAX is like:
jQuery.post(
"ajax.php",
{ 'action': 'store', 'url': url },
function(data) { alert(data); }
);
I have 2 questions:
0
comes up in the end of response?JSON.parse(response);
but not working. How to part elements of the response array in jQuery
?Question 1 is solved by using die()
after the echo
or wp_die()
in WordPress.
Can someone help me with Question 2?
Use wp_die();
at the end of the code to avoid 0
at the end in response.
$output = array(
'inputUrl' => 'some_value',
'title' => 'some_value',
'slug' => 'some_value',
);
echo json_encode( $output );
wp_die();
When the 0
is gone JSON.parse(response)
creates a data object.
Individual elements can be accessed like response.inputUrl