Ajax example (get only)

Source: Internet
Author: User

// Ajax request
Function Ajax (method, URL ){
This. method = method;
This. url = URL;
Ajax. Counter ++;
}
Ajax. Counter = 0;
Ajax. Prototype. createxhr = function () {// create XMLHttpRequest
VaR xmlhttp_request = "";
Try {
If (window. activexobject ){
For (VAR I = 5; I --){
Try {
If (I = 2 ){
Xmlhttp_request = new activexobject ('Microsoft. xmlhttp ');
}
Else {
Xmlhttp_request = new activexobject ('msxml2. XMLHTTP. '+ I +'. 0 ');
Xmlhttp_request.setrequestheader ('content-type', 'text/xml ');
Xmlhttp_request.setrequestheader ('charset', 'gb2312 ');
}
Break;
}
Catch (e ){
Xmlhttp_request = false;
}
}
}
Else if (window. XMLHttpRequest ){
Xmlhttp_request = new XMLHttpRequest ();
}
}
Catch (e) {xmlhttp_request = false ;}
Return xmlhttp_request;
}
Ajax. Prototype. encodeuri = function (_ URL ){
VaR Pos = _ URL. indexof ('? '),
HTTP = '',
ARGs = '';
If (Pos! =-1 ){
HTTP = _ URL. substring (0, POS + 1 );
ARGs = _ URL. substring (Pos + 1 );
VaR hash = args. Split ('&');
For (VAR I = 0; I Pos = hash [I]. indexof ('= ');
If (Pos! =-1 ){
Hash [I] = hash [I]. substring (0, POS + 1) + encodeuricomponent (hash [I]. substring (Pos + 1 ));
}
}
}
Else {
Return _ URL;
}
Return HTTP + hash. Join ('&');
}
Ajax. Prototype. callajax = function (senderobjid ){
VaR xmlhttp_request = This. createxhr ();
If (! Xmlhttp_request ){
Alert ('your browser does not support Ajax. ');
Return;
}
If (this. Method = 'get '){
Xmlhttp_request.open (this. method, this. encodeuri (this. url), true );
Xmlhttp_request.send (null );
VaR self = xmlhttp_request; // assign the reference to the temporary variable self
VaR objid = senderobjid; // The control that triggers
Xmlhttp_request.onreadystatechange = function (){
Thecallback (self, objid );
};
}
Else {
Throw new error ('ajax has not implemented a non-"get" transfer method. ');
}
}

Page:

VaR Ajax = new Ajax ('get', '../handler/selectdept. aspx? Parent = 01 & temp = '+ new date (). getmilliseconds ());
Ajax. callaajax (senderobjid );

......

Function thecallback (xmlhttp_request, senderobjid ){
VaR result = xmlhttp_request.readystate;
Switch (result ){
Case 0:
Case 1:
Case 2:
Case 3: break;
// A pop-up window should be displayed to indicate that the user is loading data.

......
Case 4:
// Getobj (senderobjid). removeattribute ('Disabled ');
// The pop-up window should be deleted here

If (xmlhttp_request.status = 200 ){
// Debugger;
// Bindhandler (Ajax. Counter, senderobjid); bind the event Method to the DOM
......
}
Else {
Alert ('request error. Error code: '+ xmlhttp_request.status );
Return;
}
Break;
Default: Alert ('xmlhttprequest returns a value that exceeds expectation. ');
Break;
}
}

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.