同步:客户端向服务器端发送请求的时候,用户不能进行其他操作
异步:客户端向服务器端发送请求的时候,用户可以进行其他操作
console.log("加载图片,文字1")
setTimeout(function(){
console.log("加载视频")
},1000)
console.log("加载图片,文字2")
ajax
是一种在无需重新加载整个页面的情况下
*/
var url = "http://192.168.4.18:8000/"
/* 1.创建ajax核心对象 */
var xhr = new XMLHttpRequest();
/* 2.与服务器建立连接(method,rul,async) */
xhr.open("get",url,true)
/* 3.发起请求 */
xhr.send()
/* 4.响应 */
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var txt = xhr.responseText;
var obj = JSON.parse(txt);
console.log(obj)
app.innerHTML = obj.name
}
}
封装的ajax
function ajax({
url,
method,
success
}){
var xhr = new XMLHttpRequest()
xhr.open(method,url,true)
xhr.send()
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var result = JSON.parse(xhr.responseText);
success(result);
}
}
}
解构
function go({method,url}){
var res = 10;
console.log(method);
console.log(url)
}
go({
method:"get",
url:"http",
success:res=>{
console.log(res)
}
})
get/post
get
1.用来请求数据
2.请求是放在URL地址里面的对用户是可见的
3.他发送的信息也是有限的
post
1.提交数据
2.发送的数据对用户来说是不可见的
3.发送的数据理论上是没有限制的