0

I wanna to use post function of jQuery to send data to php code,the php code echo json.

$array = array(
           'error_no' => 0;
           'error' => 'error number 0'
              );
echo (json_encode($array));

that is my php code,now how should I get post somethings to this page and access error_no element in javascript var???

1 Answer 1

3

Add this before echo, so jQuery knows that this is JSON:

header('Content-Type: application/json');

And then in javascript:

$.post('/the/url.php', post_data, function(json) {
    alert(json.error_no);
});
Sign up to request clarification or add additional context in comments.

2 Comments

post_data should be at json format? how should I convert it in javascript to json?
post_data should be either a javascript hash (like {param: value}, or a query string like param=value&param2=value2 or anything returned by .serialize())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.