Example code _ json

Source: Internet
Author: User
Sometimes we need ajax to process json data returned by php, which is suitable for users who often use php for development. For those who need it, refer to test.html.

The Code is as follows:


Click
Function ajax_init ()
{
Var ajax = false;
Try {
Ajax = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (e ){
Try {
Ajax = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (E ){
Ajax = false;
}
}
If (! Ajax & typeof XMLHttpRequest! = 'Undefined '){
Ajax = new XMLHttpRequest ();
}
Return ajax;
}
Function post_data ()
{
Var url = "/server/xml. php ";
Var show_check = document. getElementById ("show_check ");
Var show_flag = document. getElementById ("show_flag ");
Var ajax = ajax_init ();
Ajax. open ("GET", url, true );
// Var img = "";
Ajax. onreadystatechange = function ()
{
If (ajax. readyState = 4 & ajax. status = 200)
{
Var ret = ajax. responseText;
Var show_info = eval (ret );
Show_check.innerHTML = show_info [0]. no1; // bsd
Show_flag.innerHTML = show_info [0]. no2; // wuddy
// Alert ("responseXML's value:" + info [0]. firstChild. data );
}
/* Else
{
Show. innerHTML = img;
}*/
}
Ajax. send (null );
}


Json. php

The Code is as follows:


/* If $ info = array ('no1' => 'bsd', 'no2 '=> 'wuddy', 'no3' => 'xie') is written here ');*/
/* Similarly, the html file must be var show_info = eval ('[' + ret + ']'); all return an object */
$ Info = array ('no1' => 'bsd', 'no2 '=> 'wuddy', 'no3' => 'xie '));
$ Userinfo = json_encode ($ info );
Echo $ userinfo;
?>

Related Article

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.