面試先是問(wèn)了文件流下載燃领,然后又問(wèn)了ajax請(qǐng)求
原來(lái)考點(diǎn)是在:ajax無(wú)法請(qǐng)求流文件,需要使用XMLHttpRequest請(qǐng)求直奋。
ajax請(qǐng)求的五個(gè)步驟检疫。
1.創(chuàng)建XMLHttpRequest對(duì)象
2.注冊(cè)回調(diào)函數(shù)
3.配置請(qǐng)求信息
4.發(fā)送請(qǐng)求
5.創(chuàng)建回調(diào)函數(shù)
//創(chuàng)建XMLHttpRequest對(duì)象
var xmlHttp=new XMLHttpRequest();
function commentAll(){
//注冊(cè)回調(diào)函數(shù)
xmlHttp.onreadystateChange=callback1;
//配置請(qǐng)求信息
//get請(qǐng)求下參數(shù)加在url后,.ashx?methodName = GetAllComment&str1=str1&str2=str2
xmlHttp.open("post", "/ashx/myzhuye/Detail.ashx?methodName=GetAllComment", true);
//post請(qǐng)求下需要配置請(qǐng)求頭信息
//xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//第四步鸵贬,發(fā)送請(qǐng)求,post請(qǐng)求下俗他,要傳遞的參數(shù)放這
xmlHttp.send("methodName = GetAllComment&str1=str1&str2=str2");//"
}
//第五步,創(chuàng)建回調(diào)函數(shù)
function callback1() {
if (xmlHttp.readyState == 4)
if (xmlHttp.status == 200) {
//取得返回的數(shù)據(jù)
var data = xmlHttp.responseText;
//json字符串轉(zhuǎn)為json格式
data = eval(data);
$.each(data,
function(i, v) {
alert(v);
});
}