Jquery and PHP Ajax JSON

Source: Internet
Author: User

No cache, no wrong version
$. Ajax ({
Type: "Get ",
URL: "index. php ",
Cache: false,
Data: "con = add & act = _ search & Key =" + key + "& id =" + id,
Datatype: "JSON ",
Success: function (MSG ){
Bindgrouplist (MSG );
}
});

// Bind
Function bindgrouplist (result)
{
VaR eles = Document. Forms ['theform']. elements;
Eles ['group _ id']. Length = 1;
For (I = 0; I <result. content. length; I ++)
{
VaR opt = Document. createelement ('option ');
Opt. value = result. content [I]. ID;
Opt. Text = result. content [I]. Name;
Eles ['group _ id']. Options. Add (OPT );
}
}

Cache
$. Getjson ("? Con = add & act = _ search & Key = "+ key +" & id = "+ id, function (data ){
// Bindgrouplist (data)
});

===== PHP make_json_result ======
/**
* Create a data in JSON format
*
* @ Access public
* @ Param string $ content
* @ Param integer $ Error
* @ Param string $ message
* @ Param array $ append
* @ Return void
*/
Function make_json_response ($ content = '', $ error =" 0 ", $ message ='', $ append = array ()){
$ Res = array (
'Error' => $ error,
'Message' => $ message,
'Content' => $ content
);
If (! Empty ($ append )){
Foreach ($ append as $ key => $ Val ){
$ Res [$ key] = $ val;
}
}
$ Val = json_encode ($ res );

Exit ($ Val );
}

/**
*
*
* @ Access public
* @ Param
* @ Return void
*/
Function make_json_result ($ content, $ message = '', $ append = array ()){
Make_json_response ($ content, 0, $ message, $ append );
}

 

Return make_json_result ($ list );

 

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.