Ajax function code for beginners

Source: Internet
Author: User

Copy codeThe Code is as follows:
/*
Call method:
1. POST Method
Var txt = escape (sender. value); // document. getElementById ("<% = txtName. ClientID %>"). value );
Var data = "name =" + txt + "& pwd =" + txt;
Var option = {"url": "handler/Handler. ashx"
, "Action": "POST"
, "Callback": function (){
If (xmlHttp. readyState = 4) {// the server sends a response
If (xmlHttp. status = 200) {// The service responds correctly
Alert (xmlHttp. responseText );
}
XmlHttp = null; // reclaim Resources
}
}
, "Data": data
};
Ajax (option );
2. GET Method
Var txt = escape (sender. value); // document. getElementById ("<% = txtName. ClientID %>"). value );
Var option = {"url": "handler/Handler. ashx & name =" + txt + "& pwd =" + txt
, "Action": "POST"
, "Callback": function (){
If (xmlHttp. readyState = 4) {// the server sends a response
If (xmlHttp. status = 200) {// The service responds correctly
Alert (xmlHttp. responseText );
}
XmlHttp = null; // reclaim Resources
}
}
};
Ajax (option );
*/
Function ajax (option ){
CreateXMlHttpRequest (); // create an xmlHttpRequest object
If (option! = Null & option! = Undefined ){
If (option. url = null & option. url = undefined ){
XmlHttp = null;
Alert ("the required parameter option. url is missing ");
Return;
}
If (option. action = null & option. action = undefined ){
XmlHttp = null;
Alert ("the required parameter option. action is missing ");
Return;
}
XmlHttp. open (option. action, option. url, true );
If (option. contentType! = Null & option. contentType! = Undefined ){
XmlHttp. setRequestHeader ("Content-Type", option. contentType );
} Else {
XmlHttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
}
If (option. callback! = Null & option. callback! = Undefined ){
XmlHttp. onreadystatechange = option. callback;
}
If (option. action. toUpperCase () = "POST "){
XmlHttp. send (option. data );
} Else {
XmlHttp. send (null );
}
}
}
Var xmlHttp; // it is best to reclaim xmlHttp = null after the call is complete;
/* Get element */
Function g (arg ){
Var t = document. getElementById (arg );
If (null! = T & t! = Undefined ){
Return t;
}
T = document. getElementsByName (arg );
If (null! = T & t! = Undefined ){
Return t;
}
T = document. getElementsByTagName (arg );
If (null! = T & t! = Undefined ){
Return t;
}
}
/* Create an ajax request object */
Function createXMlHttpRequest (){
Try {// Firefox, Chrome, Surfri, Opera + 8
XmlHttp = new XMLHttpRequest ();
}
Catch (ie ){
Try {// IE6 +
XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (ie ){
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
}
Return xmlHttp;
}

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.