当前位置: 首页 > news >正文

Ajax (1)

什么是Ajax:

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

axios库地址:

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

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

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script><script>axios({url: 'http://hmajax.itheima.net/api/province'}).then(result => {console.log(result)console.log(result.data.list)})</script>

获取如下:

展示到页面:

<body><p></p><script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script><script>axios({url: 'http://hmajax.itheima.net/api/province'}).then(result => {const p=document.querySelector('p')p.innerHTML=result.data.list.join('<br>')})</script>
</body>

认识URL

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

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

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

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

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

URL查询参数

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

语法:

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

axios-查询参数

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

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script><script>axios({url: 'http://hmajax.itheima.net/api/city',params: {pname: '河北省'}}).then(result => {console.log(result)})</script>

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

地区查询:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport"content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"><title>~</title><link rel="shortcut icon" href="https://www.bilibili.com/favicon.ico"><link rel="stylesheet" href="css/初始化表.css"><link rel="stylesheet" href="bootstrap\css\bootstrap.min.css"><meta name="keywords" content="..." /><style>/*写代码时始终要考虑权重问题!*/@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;}</style>
</head><body><div class="center-block"><form class="form-horizontal"><div class="form-group"><label for="inputEmail3" class="col-sm-2 control-label">Province</label><div class="col-sm-10"><input type="text" class="form-control input1" id="inputEmail3" placeholder="Province"></div></div><div class="form-group"><label for="inputPassword3" class="col-sm-2 control-label">City</label><div class="col-sm-10"><input type="text" class="form-control input2" id="inputPassword3" placeholder="City"></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><button class="btn btn-default" type="button">查询</button></div></div></form><p>地区列表:</p><ul class="list-group"></ul></table></div><script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script><script>const input1 = document.querySelector('.input1')const input2 = document.querySelector('.input2')const btn = document.querySelector('.btn')btn.addEventListener('click', () => {let pname = input1.valuelet cname = input2.valueaxios({url: 'http://hmajax.itheima.net/api/area',params: {pname: pname,cname: cname}}).then(result => {let list = result.data.listlet str = list.map(item => `<li class="list-group-item">${item}</li>`).join('')console.log(str)document.querySelector('.list-group').innerHTML = str})})</script>
</body></html>

常用请求方法

资源的操作:

get:获取数据

post:提交数据

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

data:提交数据

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

axios错误处理

语法:

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

<script>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)})</script>

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

这与请求报文有关

HTTP协议-请求报文

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

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

请求报文的组成:

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

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

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

请求体:发送到资源

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

响应报文

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

响应报文的组成:

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

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

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

响应体:返回的资源

http响应状态码

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

2xx :请求成功

4xx:客户端错误

404:服务器找不到资源

接口

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

登录界面案例:

 <style>/*写代码时始终要考虑权重问题!*/@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;}</style>
</head><body><div class="container"><form class="form-signin"><h2 class="form-signin-heading">Please sign in</h2><div class="alert " role="alert">...</div><label for="input" class="sr-only">Username</label><input type="text" id="input" class="form-control username" placeholder="Username" required autofocus><label for="inputPassword" class="sr-only">Password</label><input type="password" id="inputPassword" class="form-control password" placeholder="Password" required><div class="checkbox"><label><input type="checkbox" value="remember-me"> Remember me</label></div><button class="btn btn-lg btn-primary btn-block" type="button">Sign in</button></form></div><script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script><script>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 = 1if (username.value.length < 8 || password.value.length < 6) {Alert.classList.remove('alert-success')Alert.classList.add('alert-danger')Alert.innerHTML = '错误!'}else {Alert.classList.remove('alert-danger')Alert.classList.add('alert-success')Alert.innerHTML = '登录成功'}return false}btn.addEventListener('click', function () {let flag = MyAlert()setTimeout(() => {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 => {})})</script></body>

form-serialize.js

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

http://www.lryc.cn/news/312773.html

相关文章:

  • Python基础语法:基本数据类型(数字类型和布尔类型)
  • springboot 下载 Excel 文件的 Controller 层案例
  • RabbitMQ队列
  • Day12:信息打点-Web应用源码泄漏开源闭源指纹识别GITSVNDS备份
  • 使用正确的技术和项目管理工具来定义项目范围
  • 【C++】类型转换和IO流
  • leetCode刷题 5.最长回文子串
  • 计算机组成原理面试题
  • 「Mybatis深入三」:高级查询-模糊查询
  • LabVIEW管道缺陷智能检测系统
  • java在cmd中乱码的问题解决
  • OpenHarmony教程指南—ArkUI中组件、通用、动画、全局方法的集合
  • 第二证券|金价逼近历史高点 黄金股价值有望重估
  • 关于51单片机晶振定时问题
  • NoSQL--2.MongoDB配置(Windows版)
  • HTML静态网页成品作业(HTML+CSS)——安徽宣笔设计制作(5个页面)
  • MySQL CTEs通用表表达式:进阶学习-递归查询
  • [Java安全入门]二.序列化与反序列化
  • Dutree:Linux 文件系统磁盘使用追踪工具
  • http和https的区别是什么?
  • 学习Android的第十九天
  • C#上位机调试经验
  • BUUCTF---[极客大挑战 2019]BabySQL1
  • 0基础跨考计算机|408保姆级全年计划
  • C# 操作LiteDB
  • LeetCode 2917.找出数组中的 K-or 值:基础位运算
  • MySQL窗口函数:从理论到实践
  • Vue+SpringBoot打造考研专业课程管理系统
  • python基础第二天
  • YOLOV9论文解读