Jqueryajax returns two-dimensional arrays, online, etc,

Source: Internet
Author: User
Jqueryajax returns a two-dimensional array, online, etc. below is the ajax page where I return a string
Page
$ (Document). ready (function (){
$ ("#1"). keyup (function (){
$. Post ("action. php ", {n1: $ (" #1 "). val (), n2: $("#1 "). val ()}, function (result ){
Alert (result)
});

});
});
Action. php page
$ Txt = $ _ POST ['n1 '];
Echo 'This sw from page B, input is '. $ txt. $ _ POST ['n2'];
?>

Question: Now we need to get a php two-dimensional array through the action. php page. on page a, we can use alert to generate a js two-dimensional array. how should we implement it? (test data can be used for answers) thanks


Reply to discussion (solution)

The result of alert (two-dimensional array) is array.

Data in json format can be returned.

After interaction with JS, try to use json data format.
Php can directly use the function to output the json content.

$ (Document). ready (function (){
$ ("#1"). keyup (function (){
$. Post ("action. php ", {n1: $ (" #1 "). val (), n2: $("#1 "). val ()}, function (result ){
Alert (result. n1 + ''+ result. n2 );
}, 'Json ');

});
});
Action. php page
$ Txt = $ _ POST ['n1 '];

Echo json_encoding (array ('n1 '= >$ _ POST ['n1'], 'n2 '= >$ _ POST ['n2']);
Exit;
?>

echo json_encode(array(array('a'=>'b'),array('c'=>'d')));


alert(result[0][a]);

Not tested. it should be like this.





Php + jquery + ajax + json example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.