Ajax Return Data format

Source: Internet
Author: User

Ajax The format of the returned data in

There are three types of return data that are common in Ajax: text, XML, and JSON, respectively

Returned text format We've covered it in the previous lesson Ajax basics.

ajax.php

Form.html: The ResponseText property of the Ajax object can be used to get the returned text information

XML format

The XML file has several major features:

    1. Tags are not predefined, developers invent labels according to their needs
    2. The structure is clear and self-descriptive. The contents of the data can be seen from an XML document
    3. It's all double-labeled.
    4. Similar to HTML, also has a tree structure

XML File Example

Ajax return XML Example

Core code:

You need to modify the MIME type to Text/xml

Return data should return XML tags

Accepts the XML data returned by Ajax and processes it

Detailed code See "AJAX return XML Data Format"

JSON format

In JavaScript, there is a simple way to declare an object:

"{Property Name: Property Value, Property name: property Value}"

For example:

var i= {"username": "Xiejie", "Age": 18}

Because the JSON format is smaller and faster to transfer than XML, now AJAX returns JSON data format more

Ajax returns an example of JSON format:

ajax.php need to modify the returned data format to JSON data format

Form.html also needs to be modified

JQuery with the Ajax the application

Ajax is encapsulated in jquery. Some methods are given separately, load (), get (), post (), Ajax ()

Load () Method: Is the simplest method of jquery

The function receives 3 parameters (Url,data,callback)

Parameter type

Type

Description

Url

String

The requested URL address

Data (optional)

Object

Key/value sent to the server

Callback (optional)

callback function

callback function called when the request is complete

Example:

load.html documents to be loaded

Test.html

Sending data to the server via the Load method

In the Load method, by default if the data is not submitted to the server, it is a get way to commit, if you have to submit data to the server, it is the Post method

Example: overwriting a form validation case with the load () method

Get (): Indicates how to submit to the server as get

Post (): Indicates the way to submit to the server is post

Ajax (): The Ajax () method is the lowest-level method, that is, the above method is derived from the Ajax () method, the above method can implement the function, Ajax () can be implemented

Ajax Return Data format

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.