ajax的http请求发送函数和处理返回信息函数及实例(The Ajax HTTP requests the sending function and processes the returned information function and instances)
ajax的http请求发送函数和处理返回信息函数及实例(The Ajax HTTP requests the sending function and processes the returned information function and instances)
The definition of XMLHttpRequest / object instance
Var http_request = false;
The definition of HTTP / / reusable function request
Function send_request (method, URL, content, responseType, callback) {// function specifies processing function, initialization, sending a request
Http_request = false;
/ / to initialize the XMLHttpRequest object
If (window.XMLHttpRequest) {//Mozilla browser
Http_request = new (XMLHttpRequest);
If (http_request.overrideMimeType) {// set MiME category
Http_request.overrideMimeType ("text/xml");
}
}
Else if (window.ActiveXObject) {/ / IE browser
{try
Http_request = new ActiveXObject ("Msxml2.XMLHTTP");
Catch (E) {}
{try
Http_request = new ActiveXObject ("Microsoft.XMLHTTP");
Catch (E) {}}
}
}
(if! Http_request) {/ / abnormal, failed to create an object instance
Window.alert ("cannot create a XMLHttpRequest object instance.");
Return false;
}
If (responseType.toLowerCase) (= = text) {
//http_request.onreadystatechange = processTextResponse;
Http_request.onreadystatechange = callback;
}
Else if (responseType.toLowerCase) (= = XML) {
//http_request.onreadystatechange = processXMLResponse;
Http_request.onreadystatechange = callback;
}
{else
Window.alert ("response category parameter error. ");
Return false;
}
The way to determine the URL and / / send the request and asynchronous execution under the code
If (method.toLowerCase) (= = get) {
Http_request.open (method, URL, true);
}
Else if (method.toLowerCase) (= = post) {
Http_request.open (method, URL, true);
Http_request.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
}
{else
Window.alert ("HTTP request parameter error category. ");
Return false;
}
Http_request.send (content);
}
/ / returns the text format information processing function
Function (processTextResponse) {
If (http_request.readyState = = 4) {/ / object state
If (http_request