web123456

ajax a complete ajax request

var request;
if(window.XMLHttpRequest){
  request=new XMLHttpRequest(); //IE7+,...
}else{
  request=new ActiveXObject(""); //IE6,IE5


Send the request using XHR:
open(method,url,async) Call the XHR object, async means synchronous or asynchronous, default is true (asynchronous);
send(String) Sends the request.


("POST","",true);
("Content-type", "application/x-www-form-urlencoded"); // must be written between open and send
("name=Wang Sledgehammer&sex=Male").


i. xhr obtains response
1, responseText: get the corresponding data in string form.
2, responsXML: get the corresponding data in XML form.
3, status and statusText: return http status code in the form of numbers and text.
4, getAllResponseHeader (): Get all the response header.
5. getResponseHeader(): the value of a field in the query response.
6. readyState property: get notified when the response returns success.
(1) 0: The request is not initialized and open has not been called.
(2) 1: Server connection established, open has been called.
(3) 2: The request has been received, that is, the header information has been received.
(4) 3: The request is being processed, that is, the body of the response is received.
(5) 4: The request is complete and the response is ready, that is, the response is complete.
II. Typical xhr establishmentajaxThe process of ajax. (covers most of ajax)
1. new a xhr object.
2. Call the open method of the xhr object.
3. SEND some data.
4, the server's response process to listen to know whether the server correctly responded, then you can do some things. For example, get the content of the server's response and present it on the page.


jqueryThe ajax in the
$.ajax({
        type:"GET",
        url:"?number="+$("#keyword").val(),
dataType: "json", the type of data to be returned by the server.
        success:function(data){
           if(){
               $("searchResult").html();
           }else{
$("#searchResult").html("An error occurred:" + );
           }
        },
        error:function(jqXHR){
aler("An error has occurred:" + );
        }
});




Difference between get/post requests:


1. A GET request passes parameters following the URL, while a POST request is sent to the WEB server as the physical content of an HTTP message. Of course in an Ajax request, this distinction is invisible to the user.


2. First of all, the "GET way to submit data can only be up to 1024 bytes", because GET is submitted through the URL data, then GET can be submitted to the amount of data and the length of the URL has a direct relationship. In fact, the URL does not exist in the upper limit of the parameters of the problem, the HTTP protocol specification does not limit the length of the URL. This limitation is specific to the browser and the server to its limitations. IE's limit on the length of the URL is 2083 bytes (2K + 35). For other browsers, such as Netscape, FireFox, etc., there is theoretically no length limit, and the limit depends on operating system support. Note that this is the limit is the entire URL length, not just the length of your parameter value data.


3. The data requested by the GET method is cached by the browser so that others can read it from the browser's history, such as account numbers and passwords. In some cases, the GET method can pose serious security problems. The POST method avoids these problems.


Differences between get and post requests on the server side.


4. When the client uses a get request, the server uses it to get parameters, and when the client uses a post request, the server uses it to get parameters.


The HTTP standard includes these two methods for different purposes.POST is used to create resources, the contents of which are compiled into the content of the HTTP request. Examples include processing order forms, adding new rows of data to a database, and so on.


The GET method is used when the request has no side effects (such as performing a search); the POST method is used when the request has side effects (such as adding a row of data). A more practical problem is that the GET method can produce very long URLs, perhaps exceeding the URL length limitations imposed by some browsers and servers.


However, in the following cases, use a POST request:


Unable to use cached files (update files or databases on the server)


Send large amounts of data to the server (POST has no data size limit)


POST is more stable and reliable than GET when sending user input containing unknown characters.