The JSON format in Ajax is fully parsed with the php transmission process, and the json format is fully parsed.

Source: Internet
Author: User

The JSON format in Ajax is fully parsed with the php transmission process, and the json format is fully parsed.

What are precautions for JSON format in Ajax and php transmission?

Let's take a look at the simple and common data transmission code of JSON and php.

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 ": & quot; 123456 & quot;} '; 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, you must first obtain the AJAX object and then use the POST Request Method to connect to the PHP file. At this time, the POST method is used to request data. Therefore, you must add a request file header.

xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");This is a fixed writing method. Just write it down. <Br> the above is the test code. Create a json string and use the SEND method to pass in PHP:

var user = '{"name":"zhangwuji","pwd":"123456"}';

xhr.send("user="+user);<br><br>At this time, you should note that when constructing a JSON string for transmission, the strings inside the user should be quoted separately, and double quotes should be used in it. Otherwise, php does not think that this JSON cannot be correctly parsed. <Br> check the code in the PHP file at this time:

<? Php // receives the request data sent by the client $ user =$ _ POST ['user']; // It is a JSON string $ json_user = json_decode ($ user, true); // decodes strings in json format and converts them to the PHP variable format. // 2. use the json_encode () function echo json_encode ($ json_user); // encode the php variable format and convert it to JSON format?>

The roles of decode in json_decode and json_encode are as follows:

Decodes json strings and converts them to PHP variable formats.

And encode is

Encode the php variable format and convert it to JSON format before transmitting it back;

At this time, the work of the PHP file is over. Let's go back to the HTML file and check the onreadystatechange code that accepts the data.
:

var data = xhr.responseText; Although the PHP file is transmitted back in JSON format, we accept recyclsetext here, so we only receive a string in text format. <br> at this time, we need to use eval (); the function converts the data into JSON format.

* Use the eval () function for conversion. Use "()" to wrap it. The eval () function forcibly converts it to JSON format (javascript code) and does not use "() "Wrap it, And the eval () function recognizes it as an empty code block.

Summary

The above section describes how to comprehensively parse the JSON format in Ajax and the php transmission process. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.