Javascript-how to use the ajaxpost complex json data

Source: Internet
Author: User
{Code...} newbie, please ask why this always displays error, that is, post fails? How to change it? How can I use PHP to parse the data received in the background?
$ (Function () {$. ajax ({url: 'Check. php ', // api address data: {"head": {"name": "test",}, "body": {"age ": "2" ,}}, type: 'post', // data transmission method dataType: 'json', // data Transmission Format cache: false, success: function (data) {// callback function after successful execution. data is the returned data alert (data) ;}, error: function () {alert ('error ');}});});

Newbie, please ask why this always displays error, that is, post fails? How to change it? How can I use PHP to parse the data received in the background?

Reply content:
$ (Function () {$. ajax ({url: 'Check. php ', // api address data: {"head": {"name": "test",}, "body": {"age ": "2" ,}}, type: 'post', // data transmission method dataType: 'json', // data Transmission Format cache: false, success: function (data) {// callback function after successful execution. data is the returned data alert (data) ;}, error: function () {alert ('error ');}});});

Newbie, please ask why this always displays error, that is, post fails? How to change it? How can I use PHP to parse the data received in the background?

Encode Json as a string, send it to the background with a parameter, and decode it in the background to parse json;

Var json = {"head": {"name": "test",}, "body": {"age": "2" ,}}; var str = JSON. stringify (json); $ (function () {$. ajax ({url: 'Check. php ', // api address data: {json: escape (str)}, type: 'post', // data transmission method dataType: 'json ', // data Transmission Format: cache: false, success: function (data) {// The callback function after successful execution. data is the returned data alert (data) ;}, error: function () {alert ('error ');}});});

In this way, the complex Json object is passed as a parameter to the background.
Received by the php Background: The value of POST ["json"] is decoded and parsed;

Json format error. comma is not required after "test" and "2 ".

Json encode is converted to a string, and json_decode on the php side is enough, for example
Data: {values: $. jsonEncode ({header:...})} // I forgot how to use jsonencode on the js end. check it myself.
Then
$ Parameters = json_decode ($ _ POST ['values']);

PHP is used for background reception:json_decode(file_get_contents('php://input'), true);

First, errror indicates that your server does not return data in JSON format,
Then, "head", "body" should be followed by a string,
Finally, on the PHP side, You Can $ _ POST ['head'], $ _ POST ['body'] to obtain the head and body strings.

ageIs there a space behind it? RemembertypeIt should bePOSTAnddataTypeAre you sure you have any questions?

$ (Function () {$. ajax ({url: 'Check. php ', // api address data: {"head": {"name": "test",}, "body": {"age ": "2" ,}}, type: 'post', // data transmission method dataType: 'application/json', // Data Transmission Format cache: false, success: function (data) {// The callback function after successful execution. data is the returned data alert (data) ;}, error: function () {alert ('error ');}});});

The format is incorrect. Remove the commas (,) after 'test' and '2'. Try again.

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.