Ajax前後台互動函數

來源:互聯網
上載者:User

標籤:request   erro   data   ams   code   append   ajax   參數   send   

function AJAX(obj){
//做網路請求的時候參數以對象的形式傳遞進來
//我們規定obj裡麵包含屬性:url,
//請求方式type,
//date前端給後端傳遞的參數
//回呼函數success
//回呼函數error
//跨域的回呼函數----callBack
//後台接受回呼函數的key值
if (obj.callBack&&obj.key) {
var sc=document.createElement("script");
document.documentElement.appendChild(sc);
sc.src=obj.url+"?"+obj.key+"=callBack";
return;
}
var ajaxObj=null;
if (window.XMLHttpRequest) {
ajaxObj=new XMLHttpRequest();
}else{
ajaxObj=new ActiceXObject("Microsoft.XMLHTTP");
}
ajaxObj.onreadystatechange=function(){
if (ajaxObj.readyState==4) {

if (ajaxObj.status>=200 && ajaxObj.status<300|| ajaxObj.status == 304) {
if (obj.success) {
// obj.success(JSON.parse(ajaxObj.responseText));
obj.success(ajaxObj.responseText);
}

}else{
if (obj.error) {obj.error();}

}
}

 

}
// type轉化為小寫
var type=obj.type||"get";
type=type.toLowerCase();
var params="";
if (obj.data) {
for(var key in obj.data){
params+=key+"="+obj.data[key]+"&";
}
params.slice(0,params.length-1);
}

if (type=="get") {
//判斷是否傳遞了參數
ajaxObj.open(type,obj.url+"?"+params,true);
ajaxObj.send();
}else{
ajaxObj.open(type,obj.url,true);
ajaxObj.setRequestHeader("Content=Type","application/x-www-form-urlencoded");
ajaxObj.send(params);
}

}

Ajax前後台互動函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.