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:
- Tags are not predefined, developers invent labels according to their needs
- The structure is clear and self-descriptive. The contents of the data can be seen from an XML document
- It's all double-labeled.
- 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