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

接口调用方式 -- 总结

        原生ajax

        基于jQuery的ajax

        axios

jquery的请求

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
</head>
<body><script>$.ajax({url:"http://localhost:3000/api/getmsg",// type:"get",//请求方式success(res){console.log(res)},error(err){console.log(err)}})$.get('http://localhost:3000/api/getmsg',(res)=>{console.log(res)})// $.post</script>
</body>
</html>

promise的其他方法

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
</head>
<body><script>try {console.log('a',a)let a = 8} catch (error) {console.log('error',error)}function fun(URL,TYPE,Data){return new Promise((resolve,reject)=>{$.ajax({url:`http://localhost:3000/${URL}`,type:TYPE,data:Data,success(res){resolve(res)},error(err){reject(err)}})})}//  console.log(fun())// .then()得到异步任务正确的结果fun("api/getmsg",'get').then((res)=>{console.log('then里面的res',res)},(err)=>{console.log('then里面的err',err)// .catch()获取异常信息}).catch(err=>{console.log('err',err)// .finally()成功与否都会执行}).finally(()=>{console.log('不管成功还是失败都走他')})</script>
</body>
</html>

promise的all方法和race方法

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
</head>
<body><script>function fun(URL,TYPE='get',Data){return new Promise((resolve,reject)=>{$.ajax({url:`http://localhost:3000/${URL}`,type:TYPE,data:Data,success(res){resolve(res)},error(err){reject(err)}})})}let promise1 =  fun('api/promisedata1')let promise2 =  fun('api/promisedata2')let promise3 =  fun('api/promisedata3')Promise.all([promise1,promise2,promise3]).then(res=>{// 把你得到的数据都放到一个数组里面了console.log(res)})Promise.race([promise1,promise2,promise3]).then(res=>{ // 谁跑的快就运行谁就返回谁console.log(res)})</script>
</body>
</html>

aixos的基本使用

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body><script>// get请求axios.get('http://localhost:3000/api/getmsg').then(res=>{console.log(res)})axios.get('http://localhost:3000/api/getaxios3',{params:{id:999}}).then(res=>{console.log(res)})//post请求axios.post('http://localhost:3000/api/postmsg',{firstName: 'Fred',lastName: 'Flintstone'}).then(res=>{console.log(res)})axios.get('http://localhost:3000/api/getaxios4/99999').then(res=>{console.log(res)})axios.delete('http://localhost:3000/api/deleteaxios1',{params:{name:'小明'}}).then(res=>{console.log(res)})// delete请求和get传参一样// put请求和post请求传参一样axios.put('http://localhost:3000/api/putaxios1',{name:'小明'}).then(res=>{console.log(res)})</script>
</body>
</html>

axios的全局配置

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body><script>//配置公共的请求头axios.defaults.baseURL = 'http://localhost:3000';axios.defaults.headers['toekn'] = 'token'axios({url:"/api/getaxios3",params:{tel:"1008811"}}).then(res=>{console.log(res)})</script>
</body>
</html>

aixos的拦截器

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body><script>// 添加请求拦截器// 请求拦截器的作用是在请求发送前进行一些操作// 例如在每个请求体里加上token,统一做了处理如果以后要改也非常容易axios.interceptors.request.use(function (config) {// 在发送请求之前做些什么console.log(config);config.headers['xiao']='xiao'//这里一定要return   否则配置不成功 return config;}, function (error) {// 对请求错误做些什么return Promise.reject(error);});// 添加响应拦截器// 响应拦截器的作用是在接收到响应后进行一些操作// 例如在服务器返回登录状态失效,需要重新登录的时候,跳转到登录页axios.interceptors.response.use(function (response) {// 对响应数据做点什么console.log(response);return response.data;}, function (error) {// 对响应错误做点什么return Promise.reject(error);});axios.defaults.baseURL = 'http://localhost:3000';// axios.defaults.headers['toekn'] = 'token'axios({url:"/api/getaxios3",params:{tel:"1008811"}}).then(res=>{console.log(res)})</script>
</body>
</html>


http://www.mrgr.cn/news/18486.html

相关文章:

  • Cortex-A7的运行模式和寄存器组详解
  • 实战项目-快速实战-springboot dataway
  • 相机常见名词详解
  • String和StringBuffer互转
  • 【C语言必学知识点六】自定义类型——内存对齐与位段
  • 《系统架构设计师教程(第2版)》第17章-通信系统架构设计理论与实践-02-广域网网络架构
  • 给你的头像加个口罩网站html源码
  • MySQL数据库管理系统下载安装
  • 认知杂谈29
  • AI耳机是不是好赛道
  • 第J2周:ResNet50V2算法实战与解析(pytorch版)
  • 使用SymbolGlyph和SymbolSpan在HarmonyOS中实现高级图标效果
  • 技术献文-小白如何正确使用Linux(3-高级篇)
  • Python对音频进行频谱分析
  • 美团代付支持多模板全开源多种支付通道 多模版三合一源码附教程
  • 【算法】-贪心算法
  • Django 第十三课 -- Form 组件
  • 基于yolov8的路面垃圾检测系统python源码+onnx模型+评估指标曲线+精美GUI界面
  • SUSE Linux下编译Nginx报错:recipe for target ‘install‘ failed
  • 团队动力之社会比较理论