XMLHttpRequest of Ajax_ajax related

Source: Internet
Author: User
Call
function (URL) {
var XML = new XMLHttpRequest ();
Xml.onreadystatechange = function () {
if (xml.readystate = 4)
{
if (Xml.status = 200)
{
Xml.responsetext
}
Else
{
Xml.statustext
}
}
Else
{
In Data Loading ...
}
};
Xml.open ("Get", URL);
Xml.send (NULL);
};
Define XMLHttpRequest adapt to different browsers
IE Support
if (window. ActiveXObject &&!window. XMLHttpRequest)
{
Window. XMLHttpRequest = function ()
{
var msxmls = new Array (' msxml2.xmlhttp.5.0 ', ' msxml2.xmlhttp.4.0 ', ' msxml2.xmlhttp.3.0 ', ' msxml2.xmlhttp ', ' Microsoft.XMLHTTP ');
for (var i = 0; i < msxmls.length; i++)
{
Try
{
return new ActiveXObject (Msxmls[i]);
}
catch (E)
{
}
}
return null;
};
}

Opera Support
if (Window.opera &&!window. XMLHttpRequest)
{
Window. XMLHttpRequest = function ()
{
this.readystate = 0; 0=uninitialized,1=loading,2=loaded,3=interactive,4=complete
This.status = 0; HTTP Status Codes
This.statustext = ';
This._headers = [];
this._aborted = false;
This._async = true;
This._defaultcharset = ' iso-8859-1 ';
This._getcharset = function ()
{
var charset = _defaultcharset;
var contentType = this.getresponseheader (' Content-type '). toUpperCase ();
val = contenttype.indexof (' charset= ');
If (Val!=-1)
{
CharSet = Contenttype.substring (val);
}
val = Charset.indexof (';');
If (Val!=-1)
{
CharSet = charset.substring (0, Val);
}
val = Charset.indexof (', ');
If (Val!=-1)
{
CharSet = charset.substring (0, Val);
}
return charset;
};

This.abort = function ()
{
This._aborted = true;
};

This.getallresponseheaders = function ()
{
return This.getallresponseheader (' * ');
};

This.getallresponseheader = function (header)
{
var ret = ';
for (var i = 0; i < this._headers.length; i++)
{
if (Header = = ' * ' | | this._headers[i].h = header)
{
RET = + this._headers[i].h + ': ' + this._headers[i].v + ' \ n ';
}
}
return ret;
};

This.getresponseheader = function (header)
{
var ret = Getallresponseheader (header);
var i = ret.indexof (' \ n ');
if (I!=-1)
{
ret = ret.substring (0, I);
}
return ret;
};

This.setrequestheader = function (header, value)
{
This._headers[this._headers.length] = {h:header, v:value};
};

This.open = function (method, URL, async, user, password)
{
This.method = method;
This.url = URL;
This._async = true;
this._aborted = false;
This._headers = [];
if (arguments.length >= 3)
{
This._async = async;
}
if (Arguments.length > 3)
{
Opera.posterror (' Xmlhttprequest.open ()-User/password not supported ');
}
This.readystate = 1;
if (This.onreadystatechange)
{
This.onreadystatechange ();
}
};

This.send = function (data)
{
if (!navigator.javaenabled ())
{
Alert ("Xmlhttprequest.send ()-Java must be installed and enabled.");
Return
}
if (This._async)
{
settimeout (this._sendasync, 0, this, data);
}
Else
{
This._sendsync (data);
}
};

This._sendasync = function (req, data)
{
if (!req._aborted)
{
Req._sendsync (data);
}
};

This._sendsync = function (data)
{
This.readystate = 2;
if (This.onreadystatechange)
{
This.onreadystatechange ();
}
var url = new Java.net.URL (new Java.net.URL (window.location.href), this.url);
var conn = url.openconnection ();
for (var i = 0; i < this._headers.length; i++)
{
Conn.setrequestproperty (This._headers[i].h, THIS._HEADERS[I].V);
}
This._headers = [];
if (This.method = = ' POST ')
{
POST data
Conn.setdooutput (TRUE);
var wr = new Java.io.OutputStreamWriter (Conn.getoutputstream (), This._getcharset ());
Wr.write (data);
Wr.flush ();
Wr.close ();
}
Read response headers
Note:the Getheaderfield () methods always return nulls for me:(
var gotcontentencoding = false;
var gotcontentlength = false;
var gotcontenttype = false;
var gotdate = false;
var gotexpiration = false;
var gotlastmodified = false;
for (var i = 0;; i++)
{
var hdrname = Conn.getheaderfieldkey (i);
var hdrvalue = Conn.getheaderfield (i);
if (Hdrname = = NULL && Hdrvalue = null)
{
Break
}
if (hdrname!= null)
{
This._headers[this._headers.length] = {h:hdrname, v:hdrvalue};
Switch (Hdrname.tolowercase ())
{
Case ' content-encoding ': gotcontentencoding = true; Break
Case ' content-length ': Gotcontentlength = true; Break
Case ' Content-type ': Gotcontenttype = true; Break
Case ' Date ': Gotdate = true; Break
Case ' Expires ': Gotexpiration = true; Break
Case ' last-modified ': gotlastmodified = true; Break
}
}
}
Try to fill on any missing header information
var Val;
val = conn.getcontentencoding ();
if (val!= null &&!gotcontentencoding) This._headers[this._headers.length] = {h: ' content-encoding ', v:val};
val = Conn.getcontentlength ();
If (Val!=-1 &&!gotcontentlength) This._headers[this._headers.length] = {h: ' Content-length ', v:val};
val = Conn.getcontenttype ();
if (val!= null &&!gotcontenttype) This._headers[this._headers.length] = {h: ' Content-type ', v:val};
val = Conn.getdate ();
if (Val!= 0 &&!gotdate) this._headers[this._headers.length] = {h: ' Date ', V: (New Date (Val)). toUTCString ()};
val = Conn.getexpiration ();
if (Val!= 0 &&!gotexpiration) this._headers[this._headers.length] = {h: ' Expires ', V: (New Date (Val)). toUTCString ()};
val = conn.getlastmodified ();
if (Val!= 0 &&!gotlastmodified) this._headers[this._headers.length] = {h: ' last-modified ', V: (New Date (Val)). toUTCString ()};
Read response data
var reqdata = ';
var stream = Conn.getinputstream ();
if (stream)
{
var reader = new Java.io.BufferedReader (stream, This._getcharset ()) (new Java.io.InputStreamReader);
var line;
while (line = Reader.readline ())!= null)
{
if (this.readystate = 2)
{
This.readystate = 3;
if (This.onreadystatechange)
{
This.onreadystatechange ();
}
}
Reqdata + = line + ' \ n ';
}
Reader.close ();
This.status = 200;
This.statustext = ' OK ';
This.responsetext = Reqdata;
This.readystate = 4;
if (This.onreadystatechange)
{
This.onreadystatechange ();
}
if (this.onload)
{
This.onload ();
}
}
Else
{
Error
This.status = 404;
This.statustext = ' not Found ';
This.responsetext = ';
This.readystate = 4;
if (This.onreadystatechange)
{
This.onreadystatechange ();
}
if (this.onerror)
{
This.onerror ();
}
}
};
};
}

ActiveXObject emulation
if (!window. ActiveXObject && windows. XMLHttpRequest)
{
Window. ActiveXObject = function (type)
{
Switch (Type.tolowercase ())
{
Case ' microsoft.xmlhttp ':
Case ' msxml2.xmlhttp ':
Case ' msxml2.xmlhttp.3.0 ':
Case ' msxml2.xmlhttp.4.0 ':
Case ' msxml2.xmlhttp.5.0 ':
return new XMLHttpRequest ();
}
return null;
};
}
Click here to download ajax.js

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.