Ajax for Beginners Ajax function code _ajax related

Source: Internet
Author: User
Copy Code code as follows:

/*
Call Mode:
1.POST mode
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) {//server gives a response
if (Xmlhttp.status = 200) {//service correct response
alert (Xmlhttp.responsetext);
}
XmlHttp = null; Recycle resources
}
}
, "Data": Data
};
Ajax (option);
2.GET mode
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) {//server gives a response
if (Xmlhttp.status = 200) {//service correct response
alert (Xmlhttp.responsetext);
}
XmlHttp = null; Recycle resources
}
}
};
Ajax (option);
*/
function Ajax (option) {
Createxmlhttprequest (); Creating XMLHttpRequest Objects
if (option!= null && option!= undefined) {
if (Option.url = = NULL && Option.url = = undefined) {
XmlHttp = null;
Alert ("Lack of necessary parameter Option.url");
Return
}
if (option.action = = NULL && option.action = = undefined) {
XmlHttp = null;
Alert ("Lack of necessary parameter option.action");
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 recycle the xmlHttp = null after the call completes;
/* 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 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.