ajax的get请求(不带token)
var xhr = new XMLHttpRequest();
xhr.open('get','http://localhost:8888/test/first?name=zs&password=1234');
xhr.send();
xhr.onreadystatechange = function(){
if(xhr.readyState==4&xhr.status==200){
console.log(xhr.responseText);
}
}
ajax的post请求(不带token)
var xhr = new XMLHttpRequest();
xhr.open('post','http://localhost:8888/test/first');
xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
xhr.send("name=zs&password=1234");
xhr.onreadystatechange = function(){
if(xhr.readyState==4&xhr.status==200){
console.log(xhr.responseText);
}
}
问题
- 哪个代码是异步的?
- get和post请求携带请求参数时的不同点
- 封装时哪些可以作为请求参数
- 如何在函数外部取出函数内部的响应结果
- onreadystatechange这个事件什么时候会触发
请求状态码发生改变 ()
- 响应的结果xhr.responseText 的类型是什么类型?
字符串
ajax的get请求(带token)
- token可以表示成身份令牌,在登陆成功的时候,服务器颁发的, 有些接口必须在登陆成功的时候才可以连接(购物车,获取用户信息)
var xhr = new XMLHttpRequest();
xhr.open('get','http://localhost:8888/users/info?name=zs&password=1234');
xhr.setRequestHeader("authorization",'生成的token');
xhr.send();
xhr.onreadystatechange = function(){
if(xhr.readyState==4&xhr.status==200){
console.log(xhr.responseText);
}
}
ajax的post请求(带token)
- token可以表示成身份令牌,在登陆成功的时候,服务器颁发的, 有些接口必须在登陆成功的时候才可以连接(购物车,获取用户信息)
var xhr = new XMLHttpRequest();
xhr.open('get','http://localhost:8888/users/info?name=zs&password=1234');
xhr.setRequestHeader("authorization",'生成的token');
xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
xhr.send("name=zs&password=1234");
xhr.onreadystatechange = function(){
if(xhr.readyState==4&xhr.status==200){
console.log(xhr.responseText);
}
}
这是最近学习到的ajax的封装,但是具体的内容还需要将他们组合到一起来,然后再进行共同的封装,