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