[Timlinux] JavaScript Ajax received data converted to JSON format

Source: Internet
Author: User
Tags django server
1. Receiving data

Ajax receives data through the Xhr.responsetext property, which is a property that is not a method, and this property gets the data as a string.

2. String content

When the server sends data such as a jsonresponse ({' name ': ' Tim ', ' sex ': ' Male ') (Django Server), the front-end Xhr.responsetext property gets the string value "{' Name ': ' Tim ', ' sex ': ' Male '}, again emphasizing that this is a string

3. String conversion to JSON

The string is converted to JSON, using the parse function of json, Json_data = Json.parse (Xhr.responsetext), and then in the JavaScript code, you can directly access the Name property, Json_ Data.name, Json_data.sex.

4. Check that the dictionary is empty

JavaScript dictionary type, in fact, can be considered a JSON data, when the future check the dictionary is empty, can not call the length property of the way to determine the size of zero, one way is to traverse the dictionary property, get to the value, then set a flag bit traversal to true, otherwise false, Another method is to convert the variable through json.stringify (Json_data) to a string, and then compare it with the ' {} ' string, or null if it is equal, not equal to NULL.

[Timlinux] JavaScript Ajax received data converted to JSON format

Related Article

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.