Return processing of JSON data in "Go" Ajax

Source: Internet
Author: User

When Ajax handles complex data, it uses the JSON format. Commonly used in data queries on databases. After the database has been queried for data, the data can be converted directly to JSON format on the processing page and returned.

This article focuses on: In jquery, the JSON data is used in the Ajax callback function, and there are 3 possible problems.
Case:$.ajax ({
Type: "Post",
URL: "check.php",
Data: "Findtext=yes&name=" + $findWhat,
DataType: ' JSON ',
Success:function (data) {
Return data processing, all of the following code applies to this
}
})

The most :
Questions:
Lert (data[0]);//----------> output is {or [
It is obvious that the problem is on the datatype.
Reason:
Although the data has been processed on the processing page, the JSON is passed as text (string) in the data transfer stream, and Ajax is actually receiving text that resembles an array or an object when Ajax does not explicitly indicate the data type (DataType) as JSON.
Solution:
1. Conversion of JSON objects in the callback function (not discussed here)
2, the most direct method is to check whether there is datatype: ' JSON ', parameters.
No, plus OK;
If so, check the datatype letters and capitalization for errors.

No.2:
(data that is queried from the database, the JSON that is returned to Ajax is actually an array object.)    The object is available. To get the data)
Questions:
For (var k in data) {
alert (DATA.K);
}
The data exists and returns to normal, but is used. To get the data, you cannot get the
Reason:
The data queried by the database has a trait: the first key is a string of numeric or numeric characters, and at least one of the other keys is a string. When you convert to JSON, the data is automatically returned by the JSON object {}, not the JSON array, as long as a key is a string []. JS also treats JSON as an object, so DATA.K is no problem, but don't forget that the first set of keys is a numeric or numeric string, and the object cannot be a number attribute, that is: data.0 is illegal.
Solution:
Change to Data[k].
Then add it in the for in:
$.each (Data[k],function (Index,item) {

})
Can be used in a more general way.

No.3:
(This problem, mainly for No.2)
Questions:
For (var k in data) {
$.each (Data[k],function (Index,item) {
$ ('. Findnei '). Append (item+ ' <br/> ');
})
}
The following non-expectations occur in a particular environment:
More than 7 rows of blank data is the database data, but the value after the traversal has more function .... Such an extra value.
Reason:
Not yet clear.
Solution:
Change the Forin to a normal for loop.

Return processing of JSON data in "Go" Ajax

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.