2020-09-11

image.png
image.png

image.png
image.png

面试先是问了文件流下载,然后又问了ajax请求

原来考点是在:ajax无法请求流文件,需要使用XMLHttpRequest请求。


image.png

ajax请求的五个步骤。
1.创建XMLHttpRequest对象
2.注册回调函数
3.配置请求信息
4.发送请求
5.创建回调函数

//创建XMLHttpRequest对象
var xmlHttp=new XMLHttpRequest();
function commentAll(){
//注册回调函数
xmlHttp.onreadystateChange=callback1;
//配置请求信息
//get请求下参数加在url后,.ashx?methodName = GetAllComment&str1=str1&str2=str2
xmlHttp.open("post", "/ashx/myzhuye/Detail.ashx?methodName=GetAllComment", true);
//post请求下需要配置请求头信息
//xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

//第四步,发送请求,post请求下,要传递的参数放这
xmlHttp.send("methodName = GetAllComment&str1=str1&str2=str2");//"
}
//第五步,创建回调函数
function callback1() {
if (xmlHttp.readyState == 4)
if (xmlHttp.status == 200) {
//取得返回的数据
var data = xmlHttp.responseText;
//json字符串转为json格式
data = eval(data);
$.each(data,
function(i, v) {
alert(v);
});
}

你可能感兴趣的:(2020-09-11)