XMLHttpRequest of AJAX

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
{
// Loading data...
}
};
XML. Open ("get", URL );
XML. Send (null );
};

Define XMLHttpRequest to 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. _ defacharcharset = '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 ororted ');
}
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 in 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 (New java. Io. inputstreamreader (stream, this. _ getcharset ()));
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 & window. 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

Related Article

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.