/**
?*?Ajax函數(shù)
?*?@param?{Type}?type?get?por?post
?*?@param?{Type}?url?請求地址
?*?@param?{Type}?data?數(shù)據(jù)
?*?@param?{Type}?success?請求成功執(zhí)行函數(shù)
?*?@param?{Type}?faild?請求失敗執(zhí)行函數(shù)
?*/
function?Ajax(type,?url,?data,?success,?failed)?{
//?1.創(chuàng)建xhr對象
var?xhr?=?null;
if?(window.XMLHttpRequest)?{
xhr?=?new?XMLHttpRequest();
}?else?{
//兼容ie6-
xhr?=?new?ActiveXObject('Microsoft.XMLHTTP');
}
//type類型不分大小寫?
var?type=type.toUpperCase();
//定義隨機數(shù)后面用來清除緩存
var?random?=?Math.random();
//遍歷data用&鏈接
if?(typeof?data?==?'object')?{
var?str?=?'';
for?(var?key?in?data)?{
str?+=?key?+?'='?+?data[key]?+?'&';
}
data?=?str.replace(/&$/,?'');
}
? //2.拼接傳入的json,創(chuàng)建open請求,鏈接服務器
if?(type?==?'GET')?{
if?(data)?{
xhr.open('GET',?url?+?'?'?+?data,?true);
}?else?{
xhr.open('GET',?url?+?'?t='?+?random,?true);
}
//3.發(fā)送請求?
xhr.send();
}?else?if?(type?==?'POST')?{
xhr.open('POST',?url,?true);
xhr.setRequestHeader("Content-type",?"application/x-www-form-urlencoded");
xhr.send(data);
}
//?4.處理返回數(shù)據(jù)
xhr.onreadystatechange?=?function?()?{
if?(xhr.readyState?==?4)?{
if?(xhr.status?==?200)?{
success(xhr.responseText);
}?else?{
if?(failed)?{
failed(xhr.status);
}
}
}
};
}
//課程列表
/**
?*?
?*?@param?{Type}?pag?當前頁碼
?*?@param?{Type}?psi?數(shù)據(jù)個數(shù)
?*?@param?{Type}?type?篩選類型
?*/
var?pageNumber?=?1;
var?psizeNumber?=?20;
var?typeNumber?=?10;
function?course()?{
//定義請求數(shù)據(jù)data
var?senddata?=?{
"pageNo":?pageNumber,//當前頁碼
"psize":?psizeNumber,//每頁返回數(shù)據(jù)個數(shù)
"type":?typeNumber//篩選類型:10產(chǎn)品設計,20編程語言
}
Ajax("get",?"http://study.163.com/webDev/couresByCategory.htm",?senddata,
function?(str)?{//這里str就是responseText
//成功時,創(chuàng)建html
var?arr?=?JSON.parse(str);//轉(zhuǎn)換成js
var?contentlist?=?$('courselist');//選中節(jié)點課程列表的盒子
contentlist.innerHTML?=?"";
for?(var?i?=?0;?i?<?arr.list.length;?i++)?{
//創(chuàng)建節(jié)點div小盒子?style="float:left;width:980px;"
var?coursebox?=?document.createElement('div');
coursebox.className?=?"course";
第80行的參數(shù)為什么不是xhr.responseText?
oldwan
2016-06-07 16:32:14