• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

尚硅谷AJAX教程

武飞扬头像
代码界小菜鸟
帮助1

优点:无需刷新页面获取数据,允许你根据用户事件来更新部分页面内容

缺点:没有浏览历史,不能回退,存在跨域,SEO不友好

原生XHR请求

get请求

  1.  
    <body>
  2.  
    <button>获取数据</button>
  3.  
    <script>
  4.  
    var btn =document.querySelector("button")
  5.  
    btn.onclick=function(){
  6.  
    //1.创建对象
  7.  
    const xhr = new XMLHttpRequest();
  8.  
    //2.初始化 设置请求方法和url
  9.  
    xhr.open('GET',"https://api.uomg.com/api/comments.163?format=json");
  10.  
    //3.发送
  11.  
    xhr.send();
  12.  
    //4.是按绑定 处理服务端返回的结果
  13.  
    // on when 当....时候
  14.  
    //readystate 是 xhr 对象中的属性,表示状态 0 1 2 3 4
  15.  
    //change 改变
  16.  
    xhr.onreadystatechange = function(){
  17.  
    //判断(服务端返回了所有的结果)
  18.  
    if(xhr.readyState === 4){
  19.  
    console.log(JSON.parse(xhr.response));
  20.  
    }
  21.  
    }
  22.  
    }
  23.  
    </script>
  24.  
    </body>
学新通

post请求

  1.  
    <body>
  2.  
    <button>获取数据</button>
  3.  
    <script>
  4.  
    var btn =document.querySelector("button")
  5.  
    btn.onclick=function(){
  6.  
    //1.创建对象
  7.  
    const xhr = new XMLHttpRequest();
  8.  
    //2.初始化 设置请求方法和url
  9.  
    xhr.open('POST',"https://api.uomg.com/api/comments.163?format=json");
  10.  
    //3.发送 添加请求参数
  11.  
    xhr.setRequestHeader("city","changsha")
  12.  
    xhr.send("name=zhangsan&age=18");
  13.  
    //4.是按绑定 处理服务端返回的结果
  14.  
    // on when 当....时候
  15.  
    //readystate 是 xhr 对象中的属性,表示状态 0 1 2 3 4
  16.  
    //change 改变
  17.  
    xhr.onreadystatechange = function(){
  18.  
    //判断(服务端返回了所有的结果)
  19.  
    if(xhr.readyState === 4){
  20.  
    console.log(JSON.parse(xhr.response));
  21.  
    }
  22.  
    }
  23.  
    }
  24.  
    </script>
  25.  
    </body>
学新通

一个小知识使用原生:IE缓存,在请求上随便添加一个会变得参数

xhr.open('GET',"https://api.uomg.com/api/comments.163?format=json&t=" new Date());

超时设置和网络错误设置

  1.  
    //1.创建对象
  2.  
    const xhr = new XMLHttpRequest();
  3.  
    //超时设置2秒
  4.  
    const timeout = 2000
  5.  
    //超时回调
  6.  
    xhr.ontimeout = function(){
  7.  
    alert("网络异常请稍后再试")
  8.  
    }
  9.  
    //网络异常回调
  10.  
    xhr.onerror = function(){
  11.  
    alert("你的网络视乎出了一些问题")
  12.  
    }

取消请求

  1.  
    //取消请求 abort()方法
  2.  
    xhr.abort()

节流阀,给每一次请求都加上个标识。发送成功设为true

Jquery发送ajax

get请求

  1.  
    $.get("http:localhost/jquery",{a:100,b:200},function(data){
  2.  
    console.log(data)
  3.  
    })

post请求

  1.  
    $.post("http:localhost/jquery",{a:100,b:200},function(data){
  2.  
    console.log(data)
  3.  
    })

通用模式

  1.  
    $.ajax({
  2.  
    url:"http:localhost/jquery",
  3.  
    data:{a:100,b:200},
  4.  
    type:"GET",
  5.  
    success:function(data){
  6.  
    console.log(data)
  7.  
    }
  8.  
    })

Axios发送AjAX

get请求

axios.get("url",params:{name:'city'},headers:{age:20})

post请求

axios.post(url,{name:city})

通用

  1.  
    axios({
  2.  
    method:"POST",
  3.  
    url:url,
  4.  
    params:{name:"zs"},
  5.  
    headers:{name:"ls"},
  6.  
    data:{age:10}
  7.  
    }).then(res=>{
  8.  
    console.log(res)
  9.  
    })

fetch请求

  1.  
    fetch(url,{
  2.  
    method:"POST",
  3.  
    headers:{name:"zs"},
  4.  
    body:"username=admin&age=10"
  5.  
    }).then(res=>{
  6.  
    console.log(res)
  7.  
    })

如何解决跨域

跨域得产生:违背同源策略

解决方法一:JSONP---只能处理GET请求

学新通

 解决方法二:Jquery发送jsonp请求

  1.  
    $.getJSON("http://localhost/jquery?callback=?",function(res){
  2.  
    console.log(res)
  3.  
    })

解决方法三:服务端添加CORS

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgchigi
系列文章
更多 icon
同类精品
更多 icon
继续加载