An analysis of the JSON format in AJAX and the transfer process of PHP

Source: Internet
Author: User

  What are some of the things you should be aware of in the JSON format and the PHP transfer process in Ajax?

Let's take a look at the simple generic JSON and PHP code that transmits data

HTML file:

<input type= "button" value= "Ajax" id= "btn" >  <script>var btn = document.getElementById ("btn"); Btn.onclick = function () {var xhr = getxhr (); Xhr.open ("Post", "Test. php"); Xhr.setrequestheader ("Content-type", " Application/x-www-form-urlencoded "); var user = ' {' name ': ' Zhangwuji ', ' pwd ': ' 123456 '} '; Xhr.send (" user= "+user); Xhr.onreadystatechange = function () {if (xhr.readystate==4&&xhr.status==200) {var data = Xhr.responsetext;var JSON = eval ("(" +data+) "); Console.log (JSON);}}} function getxhr () {var xhr = null;if (window. XMLHttpRequest) {xhr = new XMLHttpRequest ();} ELSE{XHR = new ActiveXObject ("Microsoft.XMLHTTP");} return XHR;}  </script>

In the whole process to get the Ajax object, and then use the POST request to connect with the PHP file, this time with the Post method request data, so this time also add a request file header

Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); This is a fixed notation that can be written directly down.

The above is the test code, creating a JSON-formatted string and using the Send method to pass in PHP:
var user = ' {' name ': ' Zhangwuji ', ' pwd ': ' 123456 '} '; Xhr.send ("user=" +user);

At this time to notice, in the construction of the JSON string transfer when the user inside the string to use a single citation, the inside with double-cited, or PHP over there is not think you this is JSON does not correctly parse.


Now look at the code in the PHP file:
<? PHP     // receiving request data    sent by the client $user $_post [' User '];     // A string string    in JSON format $json _user = Json_decode ($user,true); // decodes a JSON-formatted string into PHP variable format    //2. Using the Json_encode () function    echo json_encode ($json _user); // encode the PHP variable format into JSON format ?>

Json_decode and json_encode Everyone should be able to see from the literal meaning, decode here is the role of

Decodes a JSON-formatted string into a PHP variable format

and encode is
The PHP variable format is encoded, converted to JSON format in the transfer back;

This time the PHP file work is over, let's go back to the HTML file to see the onreadystatechange of the data to accept this piece of code
var data = Xhr.responsetext;   Although the PHP file is transmitted back in a JSON format, but we accept this here is Respensetext so received is just a text format string
At this point we're going to use eval (); function to convert it to JSON format
* Use the Eval () function for conversion using "()" to wrap it, the eval () function forces it to be converted to JSON format (JavaScript code)  without using "()" to wrap it, and the eval () function recognizes it as an empty block of code

An analysis of the JSON format in AJAX and the transfer process of PHP

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.