Ajax event usage

Source: Internet
Author: User

 

 

Error: function (XMLHttpRequest, textstatus, errorthrown ){
Alert (XMLHttpRequest. status );
Alert (XMLHttpRequest. readystate );
Alert (textstatus );
},

 

 

Additional reading:

Send a request to the server through the XMLHTTPRequest object.
Onreadystatechange event processing function, which processes triggers from the server, rather than user triggers. The readystatechange event is executed whenever it is triggered, and the readystatechange event is triggered by the server. Onreadystatechange is triggered whenever the readystate attribute changes.
VaR request = gethttpobject ();
If (request ){
Request. onreadystatechange = dosomething;
}

Note: Do not enclose dosomething in parentheses. It indicates a function, and brackets indicate the result of function execution. Dosomethiing is a self-compiled function.


Readystate
It includes four states:
0 indicates initialization -- the OPEN function has not been called.
1. Loading -- the OPEN function has been called, but the send function has not been called.
2. The -- send function has been loaded and called.
3. Interaction: the server is sending a response.
4. Complete: the server sends a response.

In reality, it is not in that state. You can simply use it:
Function dosomething (){
If (request. readystate = 4 ){
// Do something with the response
}
}
Although onreadystatechage is triggered by the server each time, the code is executed only when readystate = 4.

 

 

Status
When the browser sends a request, the server will send headers when it sends the response. headers includes the document information:
Document Type content type (HTML, XML, and so on)
Character encoding (UTF-8, iso-8859-1, and so on ).

The most important response sent by the server is the status code:
404: "Not found"
403: "Forbidden"
500: "Internal Server error"
200: "OK" is the most common
304: "not modified"

Function dosomething (){
If (request. readystate = 4 ){
If (request. Status = 200 ){
// Indicates that the server has successfully sent a response.
}
}
}

Operabrowser sometimes returns 304, so the following code is improved:
Function dosomething (){
If (request. readystate = 4 ){
If (request. Status = 200 | request. Status = 304 ){
}
}
}

Ajax event usage

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.