Ajax and the XMLHttpRequest object

2009-03-13  来源:本站原创  分类:AJAX  人气:1748 

XMLHttpRequest object components are XMLHttp through this object, Ajax can be the same as desktop applications with the server only the exchange of data layers without having to refresh every time interface, the data do not have to deal with each of the work to the server to to do; This will not only alleviate the burden on the server also recalled the response speed, which has shortened the waiting time for users.
XMLHttpRequest Object and Ajax
At Ajax application, XMLHttpRequest object is responsible for the user to asynchronous communication and information sent to the server side and receives the server response to information and data.
Attention is required JavaScript to the server itself does not have the ability to send the request, or to use window.open () method to re-open a page, or use the XMLHttpRequest object to send the request are different, the former are ordinary interactive mode that is synchronous, then Interactive mode is asynchronous.
XMLHttpRequest object property and method.
IE5.0 onwards, developers will be able to WEB page using XMLHTTP ActiveX components within the expansion of its own function, and Mozilla1.0 and create a succession NetScape7 is XML-agent type XMLHttpRequest; for most of XMLHttpRequest and XMLHttp components are similar, methods and similar property are only part of a different property. Demonstrated the following code in IE and NetScape browser, such as the creation of XMLHttpRequest object.

<script language="javascript">
/ / Define a variable var http_request = false;
/ / IE browser, create http_request = new ActiveXObject ( "Msxml2.XMLHTTP ");// new version of IE
http_request = new ActiveXObject ( "Microsoft.XMLHTTP ");// old version of IE
/ / Netscope browser http_request = new XMLHttpRequest ();
</ script>

Are required attention at Microsoft's latest version of IE7.0 browser has also been supported XMLHttpRequest, which means that the Medium IE7.0 simultaneously two kinds of support, as well as on the way to create. AJAX in our application development must pay attention to the type of browser compatibility.
Can create is as follows:

var http_request = false;
function createXMLHttpRequest ()
(
http_request = false;
/ / Initialize the XMLHttpRequest object to start if (window.XMLHttpRequest) / / If it is window.XMLHttpRequest object (
/ / Mozilla, netscape browser http_request = new XMLHttpRequest ();
if (http_request.overrideMimeType) (/ / set the MIME types / / some versions of the browser deal with the server at the back does not contain XML mime-type head and the content of information will be error, therefore, to ensure that the back contains text / xml information.
http_request.overrideMimeType ( "text / xml");
)
)
else if (window.ActiveXObject) / / If it is window.ActiveXObject
(
/ / IE browser try
(
http_request = new ActiveXObject ( "Msxml2.XMLHTTP ");// IE newer version)
catch (e)
(
try
(
http_request = new ActiveXObiect ( "Microsoft.XMLHTTP"); / / ie older version)
catch (e) ()
)
)
if (! http_request)
(
/ / Abnormal, the failure to create object instance window.alert ( "Can not create XMLHttpRequest object instance.");
return false;
)
)

This is a simple creation of cross-browser method.

In addition XMLHttpRequest object to provide a series of attributes and methods, to server-side initiated asynchronous HTTP request, the monitor server status, and complete data on the server to respond to treatment after receiving the information back server-side data. The following is its description:
XMLHttpRequest Object Properties
onreadystatechange: specify when readyState property changes deal with the case handler, write-only property.
XMLHttpRequest Object readyState property onreadystatechange are case triggers a state of change, used to specify when the readyState property changes happen at the time of treatment case. In the use of the process, usually through the case handle onreadystatechange function name given way to the specified XMLHttpRequest Trigger Event, and in the case deal with a state function to determine the value of readyState and make the appropriate treatment.
As: http_request.onreadystatechange = processRequest; processRequest case treatment as a function of the body in the ProcessRequest function readyState status at the beginning of the implementation of 4:00.
readyState: Return the current status of the request, read-only property.
These state with a length of 4 plastic figures that state the meaning of its property is as follows:
0: not initialized (the object has been set up, but not initialized, that is not yet call the open method to create http request)
1: initialize (object has been set up, but did not call the send method to send http request)
2: Send data (send method has been called, but the current state as well as the http header unknown)
3: data transmission (which was to receive part of the data, because the first http response and incomplete response at this time through a systematic approach to obtain part of the data error)
4: Send completed (the data has finished, when the response system can acquire a complete response data)
responseBody: the body will respond to information in order to return the form unsigned byte array, read-only property.
responseText: String in the form of return server response information, read-only property.
responseXML: will respond to information formatted as a XML Document object back, read-only property.

XMLHttpRequest object methods and the meaning is as follows:
abort: cancel the current request;
Grammar: http_request.abort (); call this method, the current state of the request back uninitialized.
getAllResourceHeaders: access to all of the corresponding http header information; like JSP in the HttpServletRequest object, the access request http request header information, grammar: headers = http_request.getAllResourceHeaders ();
getResourceHeader: in response to information obtained from the specified http header information.
Grammar: head = http_request.getResourceHeader ( "header-name");
open: create a new http request, and specify the request method, URL, and authentication information (username / password).
Grammar: http_request.open (method, url, async, user, password);
async as boolean value to specify whether the request is asynchronous mode, the default is true; If true, when the state change of state will call onreadystatechange property point to the callback function. If the server requires authentication, you should specify a user name and password.
send: send a request to the http server and receive response. Create http request, you can send http request to the server, send method is called grammar: http_request.send (varBody);
VarBody parameters in order to send content to the server. If there is no content to send, varBody parameters can be omitted, but the best Writing for the null, because if omitted Writing at Firefox will error, so for http_request.send (null);. This method of synchronous or asynchronous mode depending on the open method of async parameter.
setRequestHeader: a separate request for a head set.
status: Return the current HTTP request status code, read-only property. Such as: 404 = "File Not Found", 200 = "success"
statusText: Return the current HTTP request status line, read-only property.

相关文章
  • Ajax and the XMLHttpRequest object 2009-03-13

    XMLHttpRequest object components are XMLHttp through this object, Ajax can be the same as desktop applications with the server only the exchange of data layers without having to refresh every time interface, the data do not have to deal with each of

  • AJAX - XMLHttpRequest Object Browser Support 2010-12-03

    AJAX is the XMLHttpRequest object points. Create a different browser XMLHttpRequest object is different. IE browser uses the ActiveXObject, while other browsers use the name The built-in JavaScript XMLHttpRequest object. For a different browser to cr

  • AJAX XMLHttpRequest object of acquaintance. Txt 2010-08-29

    AJAX The XMLHttpRequest object /*********************************** acquaintance ******** ************************/ AJAX XMLHttpRequest object is present for all Web 2.0 applications and technology base. Although the software vendor and open source c

  • AJAX XMLHTTPRequest object for asynchronous data exchange 2010-07-03

    <html> <head><title>Simple jsp page</title> <script type="text/javascript" src="js/verify.js"></script> </head> <body> <input type="text"><br> <input type="b

  • With the correct method to create the correct xmlhttprequest object 2010-03-21

    Let me talk about xmlhttprequest version of the problem, in IE7 and above all non-IE browsers support native xmlhttprequest object, just write new XMLHttpRequest (); on the line the problem is IE6, IE6 is called by way of MSXML ActiveX parser, but al

  • Comprehensive analysis of the XMLHttpRequest object 2010-04-02

    AJAX XMLHttpRequest object is present for all Web 2.0 applications and technology base. Although the software vendor and open source community are now providing AJAX framework in order to further simplify the use of XMLHttpRequest object; but we are

  • Comprehensive analysis of the XMLHttpRequest object (change) 2010-04-13

    AJAX XMLHttpRequest object is present for all Web 2.0 applications and technology base. Although the software vendor and open source community are now providing AJAX framework in order to further simplify the use of XMLHttpRequest object; but we are

  • IE8 + timeout attribute in the XMLHttpRequest object and ontimeout events 2010-08-17

    Ajax recently wrote a tool library, JQuery and other reference processing the request timeout. Stumbled upon IE8's XMLHttpRequest object has been to increase the support. In IE9 pre4 test also supported. If the W3C standardization can both stuff and

  • Using XMLHttpRequest object is receiving and processing XML data 2010-12-13

    1. Create a new html file. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>ajaxxml.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3&

  • XMLHttpRequest object 2010-12-24

    XMLHttpRequest object is used to exchange data with the server in the background. What is the XMLHttpRequest object? XMLHttpRequest object is used to exchange data with the server in the background. XMLHttpRequest object is the developer's dream, bec

  • XMLHttpRequest object pool 2011-09-30

    If you use and interact with the server every time a new XMLHttpRequest object on the way, the client will be very expensive memory. XMLHttpRequest object pool to achieve the general idea is: a cache array to store already created the XMLHttpRequest

  • Create XMLHttpRequest object pool with js 2010-06-12

    // Use the literal syntax defines an object :XMLHttp var XMLHttp = { // Define the first property, the property is used to cache XMLHttpRequest An array of objects XMLHttpRequestPool: [], // The object of the first method, the method used to return a

  • Browser to support a variety of ways to create XMLHttpRequest object 2011-09-27

    /* Create a new XMLHttpRequest object to talk to the Web server */ var xmlHttp = false; /*@cc_on @*/ /*@if (@_jscript_version >= 5) try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp = new ActiveXObject("Mic

  • 使用Ajax技术通过XMLHttpRequest对象完成首页登录功能 2014-12-30

    这篇文章主要介绍了使用Ajax技术通过XMLHttpRequest对象完成首页登录功能,很不错的尝试,需要的朋友可以参考下 最近使用Ajax技术通过XMLHttpRequest对象完成一个首页的登录功能! 代码如下: <script type="text/javascript"> //创建XMLHttpRequest对象 function createXMLHttpRequest(){ if(window.XMLHttpRequest){ return xmlhttpreq

  • AJAX客户端说明,XMLHttpRequest对象 2013-11-17

    在CommunityServer中运用了自己的AJAX机制,没有借助其他的辅助控件.其中客户的XMLHttpRequest对象的封装,就足以让人大饱眼福,在一般的浏览器其都能够运行AJAX.下面我们来学习学习这个咚咚,希望能给更多的人带来帮助. 首先当然是要了解一下浏览器中的XMLHttp对象了: XMLHTTP方法: 备注:客户机可以使用XMLHTTP对象发送任意的HTTP请求,接受HTTP应答,还可以对应答的XML文档进行解析. Open方法:初始化一个Msxml2.XMLHTTP请求,指定

  • Ajax通讯原理XMLHttpRequest 2014-01-18

    AJAX = 异步 JavaScript 和 XML.JavaScript不用说脚本语言,XML也不用说可扩展标记语言用于存放数据信息 显然AJax就是利用JavaScript脚本访问数据的一种技术. AJAX 使网页实现异步更新.这就是在不重新加载整个网页的情况下,对网页进行局部更新. XMLHttpRequest 是 AJAX 的关键 现在浏览器均支持 XMLHttpRequest 对象(IE5 和 IE6 使用 ActiveXObject). 向后台请求数据readyState有五个状态0

  • XMLHttpRequest object simple package 2009-09-12

    var Class = { create : function() { return function () { this .initialize.apply(this, arguments);    };   } }; var myXMLHttpRequest = Class.create(); myXMLHttpRequest.prototype = { url : null, myRequest : null, callBack : null,   initialize : functio

  • XmlHttpRequest object wrapper functions, support for ie and firefox 2010-03-18

    <script> function createXMLHttp() { var ret = null; try { ret = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) { try { ret = new ActiveXObject('Microsoft.XMLHTTP'); } catch (ee) { ret = null; } } if (!ret && typeof XMLHttpRequest != 'undef

  • AJAX入门之XMLHttpRequest慨述 2013-10-26

    在使用XMLHttpRequest对象发送请求和处理响应之前,必须先用JavaScript创建一个XMLHttpRequest对象.由于XMLHttpRequest不是一个W3C标准,所以可以采用多种方法使用JavaScript来创建XMLHttpRequest的实例.Internet Explorer把XMLHttpRequest实现为一个ActiveX对象,其他浏览器(如Firefox.Safari和Opera)把它实现为一个本地JavaScript对象.由于存在这些差别,JavaScrip

  • XMLHttpRequest object properties of the three important 2010-12-03

    onreadystatechange property onreadystatechange property there is a function of processing the server response. The following code defines an empty function, which can be set to the onreadystatechange property: xmlHttp.onreadystatechange = function ()