同步:客户端向服务器端发送请求的时候,用户不能进行其他操作
异步:客户端向服务器端发送请求的时候,用户可以进行其他操作

  1. console.log("加载图片,文字1")
  2. setTimeout(function(){
  3. console.log("加载视频")
  4. },1000)
  5. console.log("加载图片,文字2")

ajax

  1. 是一种在无需重新加载整个页面的情况下
  2. */
  3. var url = "http://192.168.4.18:8000/"
  4. /* 1.创建ajax核心对象 */
  5. var xhr = new XMLHttpRequest();
  6. /* 2.与服务器建立连接(method,rul,async) */
  7. xhr.open("get",url,true)
  8. /* 3.发起请求 */
  9. xhr.send()
  10. /* 4.响应 */
  11. xhr.onreadystatechange = function(){
  12. if(xhr.readyState == 4 && xhr.status == 200){
  13. var txt = xhr.responseText;
  14. var obj = JSON.parse(txt);
  15. console.log(obj)
  16. app.innerHTML = obj.name
  17. }
  18. }

封装的ajax

  1. function ajax({
  2. url,
  3. method,
  4. success
  5. }){
  6. var xhr = new XMLHttpRequest()
  7. xhr.open(method,url,true)
  8. xhr.send()
  9. xhr.onreadystatechange = function(){
  10. if(xhr.readyState == 4 && xhr.status == 200){
  11. var result = JSON.parse(xhr.responseText);
  12. success(result);
  13. }
  14. }
  15. }

解构

  1. function go({method,url}){
  2. var res = 10;
  3. console.log(method);
  4. console.log(url)
  5. }
  6. go({
  7. method:"get",
  8. url:"http",
  9. success:res=>{
  10. console.log(res)
  11. }
  12. })

get/post

  1. get
  2. 1.用来请求数据
  3. 2.请求是放在URL地址里面的对用户是可见的
  4. 3.他发送的信息也是有限的
  1. post
  2. 1.提交数据
  3. 2.发送的数据对用户来说是不可见的
  4. 3.发送的数据理论上是没有限制的