Note: XMLHttpRequest is the basis of Ajax, and when creating XMLHttpRequest objects, you must be in the same ' <script></script> ' tag as the Ajax method you write! Otherwise, the AJAX request will go wrong and the data cannot be returned. JAVASCRIPT/JS Ajax Post/get Request.
Javascript/js's Ajax POST request:
The code is as follows |
Copy Code |
<script type= "Text/javascript" > /* Create 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.111cn.net/t/" +math.random (); Xmlhttp.open ("POST", url,true); Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Xmlhttp.send (); xmlhttp.onreadystatechange=getlabelsok;//after sending the event, you receive the message. Call function } function Getokpost () { if (xmlhttp.readystate==1| | xmlhttp.readystate==2| | xmlhttp.readystate==3) { Local tip: In-load/process } if (xmlhttp.readystate==4 && xmlhttp.status==200) { var D=xmlhttp.responsetext; return value Processing return values } } </script> |
JAVASCRIPT/JS Ajax GET Request:
The code is as follows |
Copy Code |
<script type= "Text/javascript" > /* Create 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 sending the event, you receive the message. Call function Xmlhttp.send (); }
function Getokget () { if (xmlhttp.readystate==1| | xmlhttp.readystate==2| | xmlhttp.readystate==3) { Local tip: In Load
} if (xmlhttp.readystate==4 && xmlhttp.status==200) { var d= xmlhttp.responsetext; Processing return results
} } </script> |
ReadyState Property
Includes five possible values:
0: (uninitialized) The Send method has not yet been invoked
1: (in load) the Send method has been invoked and the request is still being processed
2: (loaded) The Send method is complete and the entire answer has been received
3: (in interaction) parsing the answer
4: (completed) The answer has been resolved and ready for next steps.
Object XMLHttpRequest
The meaning of the attribute status:
0**: not initialized
1**: Request received, continue processing
2**: Operation received, analyzed and accepted successfully
3**: Complete this request must be further processed
4**: request contains an error syntax or cannot be completed
5**: Server failed to perform a fully valid request
100--customer must continue to issue request (Continue)
101--client requires the server to convert the HTTP protocol version (switching protocols) on request
200--Transaction Success (OK)
201--prompts to know the URL of the new file (Created)
202--accept and process, but processing incomplete (accepted)
203--return information is uncertain or incomplete (non-authoritative information)
204--request received, but return information is empty (No Content)
The 205--server completes the request, and the user agent must reset the currently viewed file (reset Content)
The 206--server has completed some of the user's get requests (Partial Content)
300--requested resources can be obtained in multiple places (mult<a href= "http://ip.111cn.net/" >ip</a>le choices)
301--Delete request data (moved Permanently)
302--found request data at other addresses (Found)
303--advises customers to access other URLs or access methods (see others)
The 304--client has executed a GET, but the file has not changed (not Modified)
The resource requested by 305--must be obtained from the address specified by the server (use Proxy)
306--the code used in the previous version of HTTP, which is no longer used in the current version
307--declare the requested resource to be temporarily deleted (temporary Redirect)
400--error request, such as syntax error (Bad request)
401--Request authorization failed (unauthorized)
402--retains a valid Chargeto header response (Payment Required)
403--request not allowed (because the resource is not available because of the permissions set on the file or directory on the server) (forbidden)
404--did not find a file, query, or URL (the specified resource was not found) (not Found)
405--the method defined by the user in the Request-line field is not allowed (methods not allowed)
406--the request resource is inaccessible (not acceptable) based on the accept drag sent by the user
407--is similar to 401, the user must first be authorized on the proxy server (proxy authentication Required)
408--client does not complete the request within the time specified by the user (request Timeout)
409--the current resource state, the request cannot be completed (Conflict)
This resource is no longer available on the 410--server and no further reference address (Gone)
411--server rejects user-defined Content-length property request (Length Required)
412--one or more request header fields errors in the current request (precondition Failed)
The resource requested by 413--is larger than the size allowed by the server (Request Entity Too Large)
The 414--requested resource URL is longer than the length allowed by the server (Request-uri Too long)
415--Request Project Format (Unsupported Media Type) is not supported by the requested resource
The 416--request contains the range Request header field, there is no range indicating value within the current request resource range, and the request does not contain the If-range Request header field (requested Range not suitable)
The 417--server does not meet the expected value specified by the request Expect header field, and if it is a proxy server, it may be that the next level server does not meet the request (expectation Failed) <br/>
500--server generates internal error (Internal server error)
The requested function (not implemented) is not supported by the 501--server
502--servers are temporarily unavailable, sometimes to prevent system overload (bad Gateway)
503--server overload or suspend maintenance (Service unavailable)
504--Gateway overload, the server uses another gateway or service to respond to users, waiting time set value longer (Gateway Timeout)
505--server does not support or reject the HTTP version specified in the Support 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 (reason: Network is not known.)