Status values of ajax request methods (GET and POST) in javascript

Source: Internet
Author: User

Ajax request methods include get and post methods, but they return many Status values, and each piece of code represents a different meaning. Let's look at the meaning of various codes, I hope this will help you.

Note: XMLHttpRequest is the basis of AJAX. When creating an XMLHttpRequest object, it must be in the same '<script> </script>' tag as the ajax method you wrote! Otherwise, an error occurs in the ajax request and data cannot be returned. Javascript/js ajax POST/GET requests.

Javascript/js ajax POST request:

The Code is as follows: Copy code

<Script type = "text/javascript">
/* Create an XMLHttpRequest object */
Var xmlHttp;
Function GetXmlHttpObject (){
If (window. XMLHttpRequest ){
// Code for IE7 +, Firefox, Chrome, Opera, Safari
Xmlhttp = new XMLHttpRequest ();
} Else {// code for IE6, IE5
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Return xmlhttp;
}
// ----------- Ajax method -----------//
Function getLabelsPost (){
XmlHttp = GetXmlHttpObject ();
If (xmlHttp = null ){
Alert ('your browser does not support AJAX! ');
Return;
}
Var url = "http://www.hzhuti.com/t/" + Math. random ();
Xmlhttp. open ("POST", url, true );
Xmlhttp. setRequestHeader ("Content-type", "application/x-www-form-urlencoded ");
Xmlhttp. send ();
XmlHttp. onreadystatechange = getLabelsOK; // after an event is sent, the system receives the message and calls the function.
}
Function getOkPost (){
If (xmlHttp. readyState = 1 | xmlHttp. readyState = 2 | xmlHttp. readyState = 3 ){
// Local prompt: loading/Processing

}
If (xmlHttp. readyState = 4 & xmlHttp. status = 200 ){
Var d = xmlHttp. responseText; // Return Value
// Process the returned value
}
}
</Script>

Javascript/js ajax GET request:

The Code is as follows: Copy code

<Script type = "text/javascript">
/* Create an XMLHttpRequest object */
Var xmlHttp;
Function GetXmlHttpObject (){
If (window. XMLHttpRequest ){
// Code for IE7 +, Firefox, Chrome, Opera, Safari
Xmlhttp = new XMLHttpRequest ();
} Else {// code for IE6, IE5
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Return xmlhttp;
}
// ----------- Ajax method -----------//
Function getLabelsGet (){
XmlHttp = GetXmlHttpObject ();
If (xmlHttp = null ){
Alert ('your browser does not support AJAX! ');
Return;
}
Var id = document. getElementById ('id'). value;
Var url = "http://www.lifefrom.com? Id = "+ id +" & t/"+ Math. random ();
XmlHttp. open ("GET", url, true );
XmlHttp. onreadystatechange = favorOK; // after an event is sent, the system receives the message and calls the function.
XmlHttp. send ();
}

Function getOkGet (){
If (xmlHttp. readyState = 1 | xmlHttp. readyState = 2 | xmlHttp. readyState = 3 ){
// Local prompt: Loading

}
If (xmlHttp. readyState = 4 & xmlHttp. status = 200 ){
Var d = xmlHttp. responseText;
// Process the returned results

}
}
</Script>

ReadyState attributes

There are five possible values:

0: (not initialized) The send method has not been called

1: (loading) The send method has been called and the request is still being processed.

2: (loaded) The send method has been completed, and the entire response has been received

3: The response is being parsed.

4: (completed) the response has been resolved and is ready for further processing.

Object XMLHttpRequest

Description of attribute status:

0 **: not started

1 **: request received, continue processing

2 **: The operation is successfully received, analyzed, and accepted.

3 **: the request must be further processed.

4 **: The request contains an error syntax or cannot be completed

5 **: the server failed to execute a fully valid request.

100 -- the customer must Continue to send the request (Continue)

101 -- the client requests the server to convert the HTTP protocol version (Switching protocols) according to the request)

200 -- transaction successful (OK)

201 -- prompt to know the URL of the new file (Created)

202 -- accept and process, but not complete (Accepted)

203 -- Non-Authoritative Information)

204 -- the request is received, but the returned information is null (No Content)

205 -- when the server completes the request, the user agent must Reset the currently browsed file (Reset Content)

206 -- the server has completed some users' GET requests (Partial Content)

300 -- requested resources can be obtained in multiple locations (Mult <a href = "http://ip.knowsky.com/"> ip </a> le Choices)

301 -- Delete request data (Moved Permanently)

302 -- Found the request data (Found) at another address)

303 -- we recommend that you access Other URLs or access methods (See Other)

304 -- the client has executed GET, but the file has Not changed (Not Modified)

305 -- the requested resource must be obtained from the address specified by the server (Use Proxy)

306 -- code used in HTTP of the previous version, which is not used in the current version

307 -- declare Temporary deletion of requested resources (Temporary Redirect)

400 -- error Request, such as syntax error (Bad Request)

401 -- Request Authorization failed (Unauthorized)

402 -- retain the valid ChargeTo header response (Payment Required)

403 -- the request is not allowed (the resource is unavailable due to the permission setting of the file or directory on the server) (Forbidden)

404 -- no file, query, or URl Found (the specified resource is Not Found)

405 -- the Method defined in the Request-Line field is Not Allowed (Method Not Allowed)

406 -- the requested resource is inaccessible (Not Acceptable) based on the Accept drag sent by the user)

407 -- similar to 401, the user must first be authorized on the Proxy server (Proxy Authentication Required)

408 -- the client does not complete the Request within the specified time (Request Timeout)

409 -- unable to complete the request for the current resource status (Conflict)

410 -- this resource is no longer available on the server and there is no further reference address (Gone)

411 -- the server rejects the User-Defined Content-Length attribute request (Length Required)

412 -- one or more request header fields are incorrect in the current request (Precondition Failed)

413 -- the requested resource is larger than the size allowed by the server (Request Entity Too Large)

414 -- the requested resource URL is longer than the length allowed by the server (Request-URI Too Long)

415 -- the requested resource does not support the format of the requested project (Unsupported Media Type)

416 -- The request contains the Range request header field. There is no range indication value in the current request resource Range, and the request does Not contain the If-Range request header field (Requested Range Not Suitable)

417 -- the server does not meet the Expectation specified by the request's response CT header field. If it is a proxy server, it may be that the next-level server cannot meet the request (Expectation Failed) <br/>

500 -- the Server generates an Internal Error (Internal Server Error)

501 -- the server does Not support the requested function (Not Implemented)

502 -- the server is temporarily unavailable, sometimes to prevent system overload (Bad Gateway)

503 -- Service Unavailable)

504 -- the Gateway is overloaded. The server uses another Gateway or service to respond to the user. The waiting time is long (Gateway Timeout)

505 -- the server does Not support or reject the HTTP Version specified in the request header (HTTP Version Not Supported)

12029 -- an unknown error occurred while processing the request on the server. the status code returned from the server was: 12029 (cause: the network is disconnected. Refresh and you will know)

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.