Transfer between JS and PHP in JSON format

Source: Internet
Author: User
Tags response code

JSON is a lightweight text data interchange format. It is independent of the programming language and can be used to interact with data between unused programming languages.

The following is a simple example of two data communication using JSON.

A first example:

//javascript send data JSON data in Ajax, PHP receives JSON//Front Endvararr ={    "Name": "Xiaoming",    "Age": 16    };varJSON =json.stringify (arr);//using JSON to convert objects into JSON-formatted datavarXHR =NewXmlhttprequest;xhr.open (' Post ', './bb.php '); Xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded '); Xhr.send ("user=" + JSON);//when Content-type is set to application/x-www-form-urlencoded, the request body can send data in key1=value1&key2=value2 formXhr.onreadystatechange =function() {         if(Xhr.readystate = = 4 && (xhr.status = = | | xhr.status ==304))//response is complete and the response code is 200 or 304alert (xhr.responsetext);} ------Gorgeous split-line----------------------------------------------------
//back end<?PHP $info= $_post["User"];//The info at this time is a string$result = Json_decode ($info);//The result of this time has been reverted to objectecho $result, name;

A second example:

 //php send data JSON data JavaScript to receive JSON in AJAX  //   front end  var  xhr = new   Xmlhttprequest;xhr.open ( ' post ', './bb.php '  ' content-type ', ' application/x-www-form-urlencoded '  = function   () { if  (xhr.readystate = = 4 && (xhr . Status = = 200 | | Xhr.status ==304) {//  response complete and response code 200 or 304             Span style= "COLOR: #0000ff" >var  rst = Json.parse (Xhr.responsetext);        alert (rst.name); }};
------Gorgeous split-line----------------------------------------------------
// back end
$info = Array ("lession" = "English", "name" and "Lily");
echo Json_encode ($info);

Add a compatibility for JSON. JSON this built-in object does not exist before IE8, and if you need to use a JSON object before IE8, you need to introduce a third-party plug-in json2.js as described below. This will load the Json2.js plugin in IE7 and below, while other browsers or 8 and later versions of IE will not load this plugin:

<!--[If LTE IE 7]>       <script src= "./json2.js" ></script><![ Endif]-->

Transfer between JS and PHP in JSON format

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.