JSON string problem using $.each () to traverse background responses in jquery

Source: Internet
Author: User

When doing a practice project today, using the $.each () method to traverse a JSON string passed through the background, the Chrome browser found the following problem cannot use ' in ' operator to search for ' length ' ... Pondering for a long time, think not the solution. The front and rear code is as follows:

The JSON string is returned in the background:

$sql = "Select PID, PName, Price, pic, do, count from Jd_product, Jd_cart_detail where Pid=productid and cartid= $cid";
$result = Mysqli_query ($conn, $sql);
$list = Mysqli_fetch_all ($result, MYSQLI_ASSOC);
echo Json_encode ($list);

Reception Received and processed:

$.ajax ({
Type: "GET",
URL: "data/cart_detail_select.php",
Data: {Uid:loginuid},
Success:function (list) {
var h = "";
$.each (list, function (I, p) {
H + = '
Some code ....
`;
});
$ ("#cart >tbody"). html (h);
},
Error:function (obj) {
Alert ("Response completed with error");
Console.log (obj);
}
});

Okay, here's the problem, $.each () method, the list parameter is the JSON string from the background (echo Json_encode ($list), however, after a variety of debugging, the browser still stubbornly reported the error at the beginning of the article. Helpless, after trying, finally found a solution, although solved the problem, but I still do not understand why to do so, resolved as follows:

The list parameter, and then the Json.parse (list), is packaged once before it can run normally.

The wrong line of code has been modified as follows:

$.each (Json.parse (list), function (I, p) {}).

If a great God sees this article and knows the reason, please do not hesitate to enlighten.

JSON string problem using $.each () to traverse background responses in jquery

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.