Measure the test taker's knowledge about JSAJAXPOSTJson data.

Source: Internet
Author: User
Ask a question about JSAJAXPOSTJson data. let's talk about how PHP receives data first. I directly use JS and do not use other libraries. so please do not use other libraries as an example, I wrote an AJAX method using JS scripts as follows var & nbsp; pageNowthis. className. substr (4); var & nbsp; dd asks about the method for receiving data from JavaScript ajax post Json data.
First, I use JS directly without other libraries, so please do not use other libraries as an example.
The problem is that I wrote an AJAX method using a JS script as follows:

Var pageNow = this. className. substr (4 );
Var dID = document. getElementById ("dID"). value;
Var url = "OOXX ";
Var jsonData = {
"HdType": "3 ",
"DID": dID,
"Content": content
};
Var data = {data: jsonData };
Var contentType = "application/json ";
Var ajax = new Simple. Ajax ("POST", url, receive4, data, contentType); // receiveInfo is a method name.
Ajax. SendRequest ();

The Simple. Ajax method is encapsulated. in the method, receive4 is the AJAX callback method, contentType is the data header, and data is the data to be sent. you can probably understand it ..
For example, when I used POST to send simple data, the data format is "key1 = value1 & key2 = value2, then the method obtained by the server using PHP is $ _ POST ["key1"], or $ _ POST ["key2 "].
Now, considering that the value of value1 or value2 may contain the "&" symbol, I want to use JSON to POST the entire object to the server at a time, but now there is a problem, I don't know how to use PHP to obtain this data. I used $ _ POST ["data"] to obtain null data. directly print_r ($ _ POST) didn't get the data either, how can I obtain data through PHP?
------ Solution --------------------
Since you didn't tell us how Simple. Ajax was written, you can only talk about it in principle.
1. if post is used, the Content-Type = application/x-www-form-urlencoded statement must be included.
Otherwise, php will not parse the incoming data to the $ _ POST array.
You have issued a contentType = application/json declaration, even if your Simple. Ajax will automatically add a Content-Type = application/x-www-form-urlencoded declaration. These two competing statements will also make php at a loss.
2. the data you sent is {data: jsonData}, which is a js object. Even if the message can be sent successfully, php cannot parse the object because it does not know it. All data structures exchanged with the outside world must be serialized into strings in the format agreed by both parties.

Assume that your Simple. Ajax can convert js objects into JSON strings.
You also removed the contentType = application/json declaration.
Then you can see the element with the JSON string as the key in $ _ POST.
If you insist that you do not want to remove the contentType = application/json statement
Maybe you can use file_get_contents ('php: // input') to get the input content.

------ Solution --------------------
If Content-Type = application/json declaration exists, $ _ POST will not have data.
------ Solution --------------------
File_get_contents ('php: // input ')
But it cannot be successful.

Application/json is only used by the server to notify users that the content can be interpreted as a json object.
Just like image/png, which means this is a png image.

When a user sends data to a server, only
Application/x-www-form-urlencoded
Multipart/form-data
Text/plain
Three Declarations

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.