/*-----------------------------
ajax关键代码
将ajax封装成一个类
-----------------------------*/
function createAjax() {
var xmlHttp = false;
if (window.XMLHttpRequest) {
//mozilla
xmlHttp = new XMLHttpRequest();//ie6不支持XMLHttpRequest
if (xmlHttp.overrideMimeType) xmlHttp.overrideMimeType("text/xml");
}
else if (window.ActiveXObject) {
var MSXML = ['MSXML2.XMLHTTP.5.0','Microsoft.XMLHTTP',128)">'MSXML2.XMLHTTP.4.0',128)">'MSXML2.XMLHTTP.3.0',128)">'MSXML2.XMLHTTP'];
var i;
for(i = 0; i < MSXML.length; i++){
try{
xmlHttp = new ActiveXObject(MSXML[i]);
break;
}
catch(e){}
}
}
if (!xmlHttp) { alert("error! fail to create an ajax instance!"); return false; }
this.rep = xmlHttp;
this.sendData = function(method,url,asy,fun) {
if (asy) {
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
fun(xmlHttp);
}
if (method == "POST") {
xmlHttp.open("POST",255)">true);
xmlHttp.setRequestHeader("Conent-Type",244)">"application/x-www-form-urlencoded");
xmlHttp.send(null);
}
else {
xmlHttp.open("GET",244)">true);
}
else {
if (method == "POST") {
"POST",244)">false);
xmlHttp.setRequestHeader("Content-Type",128)">"application/x-www-form-urlencoded");
xmlHttp.send(null);
"GET",255)">false);
return xmlHttp;
}
使用:
先实例一个对象:
1: var ajax = new createAjax();
再写方法:
1: ajax.sendData("articleEdit.aspx?RemoveAttachmentID=" + attachmentID + "&NewsID=" + newsID,255)">true,255)">function(xmlhttp) {
2: if (xmlhttp.responseText != "") {
3:
4: //to do something
5:
6: }
7: })