小工具知识网

当前位置: 首页 > 行业知识 > Ajax (1)

行业知识

Ajax (1)

2026-11-25 05:01:17 来源:互联网转载

什么���Ajax:

浏览器与服务器进行数据通讯的技术,动态数据交互

axios库地址:

  

如何使用呢? 我们现有个感性的认识

      axios({      url: 'http://hmajax.itheima.net/api/province'    }).then(result => {      console.log(result)      console.log(result.data.list)    })  

获取如下:

展示到页面:

          axios({      url: 'http://hmajax.itheima.net/api/province'    }).then(result => {      const p=document.querySelector('p')      p.innerHTML=result.data.list.join('
') })

认识URL

URL是统一资源定位符,俗称网址,访问网络资源

组成: 协议、域名、资源路径

http协议:超文本传输协议,规定服务器和浏览器之间传输数据的格式

域名:标记服务器在互联网中的方位

资源路径:标记资源在服务器下具体位置

URL查询参数

浏览器提供给服务器的额外信息,让服务器返回浏览器想要的数据

语法:

http://xxx.com/xxx/xxx?参数名1=值1&参数名2=值2

axios-查询参数

语法: 使用axios提供的params选项(拿数据时的查询参数)

      axios({      url: 'http://hmajax.itheima.net/api/city',      params: {        pname: '河北省'      }    }).then(result => {      console.log(result)    })  

axios原码在运行时把参数名自动拼接到url上

地区查询:

        ~              /*写代码时始终要考虑权重问题!*/    @font-face {      font-family: 'icomoon';      src: url('fonts/icomoon.eot?au9n7q');      src: url('fonts/icomoon.eot?au9n7q#iefix') format('embedded-opentype'),        url('fonts/icomoon.ttf?au9n7q') format('truetype'),        url('fonts/icomoon.woff?au9n7q') format('woff'),        url('fonts/icomoon.svg?au9n7q#icomoon') format('svg');      font-weight: normal;      font-style: normal;      font-display: block;    }    .center-block {      display: block;      margin-left: auto;      margin-right: auto;      width: 500px;    }                      Province                                              City                                                                                    查询                      

地区列表:

    const input1 = document.querySelector('.input1') const input2 = document.querySelector('.input2') const btn = document.querySelector('.btn') btn.addEventListener('click', () => { let pname = input1.value let cname = input2.value axios({ url: 'http://hmajax.itheima.net/api/area', params: { pname: pname, cname: cname } }).then(result => { let list = result.data.list let str = list.map(item => `
  • ${item}
  • ` ).join('') console.log(str) document.querySelector('.list-group').innerHTML = str }) })

    常用请求方法

    资源的操作:

    get:get请求可以得到我们想要的具体的数据,then方法指定成功时候的回调(params)

    post:操作post请求增加一条或者多条数据,可以采用JSON的形式传输数据

    put:我们采put请求修改数据,可以具体修改某一条数据

    delete:删除数据

    method:请求方法,get可以省略

    data:提交数据

              axios({        url: 'http://hmajax.itheima.net/api/register',        method:'post',        data: {          username: 'qwertyu123',          password: '123456'        }      }).then(result => {        console.log(result)      })  

    axios错误处理

    语法:

    在then后通过 . (点)语法调用catch方法,传入回调函数并定义形参

              axios({        url: 'http://hmajax.itheima.net/api/register',        method:'post',        data: {          username: 'qwertyu123',          password: '123456'        }      }).then(result => {        console.log(result)      }).catch(error=>{        alert(error.response.data.message)      })  

    浏览器是如何把内容发送给服务器的?

    这与请求报文有关

    HTTP协议-请求报文

    http格式规定了浏览器发送及浏览器返回内容的格式

    请求报文:浏览器按照http协议要求的格式,发送给服务器的内容

    请求报文的组成:

    请求行:请求方法(如post),URL,协议

    请求头:以键值对的格式携带的附加信息,如:Content-Type

    空格:分隔请求头,空行之后是发送给服务器的资源

    请求体:发送到资源

    在浏览器中可以看到这些内容

    响应报文

    响应报文:服务器按照http协议要求的格式,返回给浏览器的内容

    响应报文的组成:

    响应行(状态行):协议,http响应状态码,状态信息

    响应头:以键值对的格式携带的附加信息,如:Content-Type

    空格:分隔响应头,空行之后是服务器返回的资源

    响应体:返回的资源

    http响应状态码

    用来表明请求是否成功完成

    2xx :请求成功

    4xx:客户端错误

    404:服务器找不到资源

    接口

    在使用AJAX与后端通讯,使用的URL,请求方法,以及参数

    登录界面案例:

         /*写代码时始终要考虑权重问题!*/    @font-face {      font-family: 'icomoon';      src: url('fonts/icomoon.eot?au9n7q');      src: url('fonts/icomoon.eot?au9n7q#iefix') format('embedded-opentype'),        url('fonts/icomoon.ttf?au9n7q') format('truetype'),        url('fonts/icomoon.woff?au9n7q') format('woff'),        url('fonts/icomoon.svg?au9n7q#icomoon') format('svg');      font-weight: normal;      font-style: normal;      font-display: block;    }    .form-control {      width: 400px;    }    .btn-block {      width: 100px;    }    .alert {      width: 400px;      height: 50px;      opacity: 0;    }              

    Please sign in

    ... Username Password Remember me Sign in const btn = document.querySelector('.btn') const username = document.querySelector('.username') const password = document.querySelector('.password') const Alert = document.querySelector('.alert') function MyAlert() { Alert.style.opacity = 1 if (username.value.length { Alert.style.opacity = 0 }, 2000) if (!flag) { return } axios({ url: 'http://hmajax.itheima.net/api/login', method: 'post', data: { username: username.value, password: password.value } }).then(result => { console.log(result) }).catch(error => { }) })

    form-serialize.js

    可以快速获取表单元素,通过解构对象获得用户信息

    上一篇:服务器cpu参数怎么记?

    下一篇:云钱袋服务器登录不上去怎么回事儿