The reason why jQuery $. post () does not enter the callback function when the return type is json and the solution is jqueryjson

Source: Internet
Author: User

The reason why jQuery $. post () does not enter the callback function when the return type is json and the solution is jqueryjson

This is really a problem that people are particularly worried about scratching the liver. It was really almost forced to give up using json as the return type, but to use html, but sometimes it is better. (Even if the problem is solved, I still want to say "mom" to let it go)

In fact, I am not familiar with json, but I am wondering that there are almost no solutions on the Internet. So although the problem is very small, I still want to share it, hoping to help the people who need it.

First, the format of $. post () is as follows:

$.post(url,data,success(data, textStatus, jqXHR),dataType)

Parameter description

Url
Required. Specifies the URL to which the request is sent.

Data
Optional. Ing or string value. It is in json format.

Success (data, textStatus, jqXHR)
Optional. The callback function executed when the request is successful.

DataType

Optional. Specifies the expected server response data type. Intelligent judgment (xml, json, script, or html) is performed by default ).

Example:

                $.post(                    "../../xxx/ajaxPost.aspx",                    {                        action: "xxx",                        id: xxx                    },                    function (data,status) {                        alert("name="+data.name+",age="+data.age);                    },                    "json");

You can access the background normally when you break a breakpoint. Until the desired operation is completed, a new json data is formed and the Response is used. the Write () method is returned, but the function () method is not used in any case. It was previously thought of as a json format problem, but it still cannot be changed.

Finally

Background:

            var result = "{\"name\":\"Michael Jordan\",\"age\":\"51\"}";            Response.Write(result);            Response.End();

Everyone knows that the json format is {"key1": "value1", "key2": "value2"} (or convert the double quotation marks in {} into single quotation marks ), however, in the C # code section, the escape character (\) must be added before the quotation marks (""). Otherwise, the callback method will never be implemented.

When the $. post () method uses json as the return type, it is strictly required to use json format to enter the callback. So ......

Run:


In jquery, when $ post returns json data, how does one obtain the status value or data value in json In the callback function?

$. Post (url, {}, function (json ){
Var o = eval ("(" + json + ")"); // convert json to object json format {status: '1', data: '2 '}
Alert (o. status );
Alert (o. data );
});

When $ post () is used in jquery for Asynchronous interaction, does the callback function not execute?

The callback function is not executed for only one reason.

Your js Code is incorrect and js Code is not executed.

If your alert ("hou ");
Can be executed, then you do not alert (data );

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.