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

springboot和vue:十一、Axios网络请求的安装引入与使用、跨域问题解决(CORS)

Axios简介与安装

  • Axios是一个基于promise的网络请求库,作用于node.js和浏览器中
  • Axios在浏览器端使用XMLHttpRequests发送网络请求,并自动完成json数据的转换
  • 安装:npm install axios
  • 官方文档:https://www.axios-http.cn/

Axios基础语法

get请求

当参数比较少时,直接在路径里面用问号拼接传入。
then里面的是个回调函数,原始形态是如下:

    axios.get("/user?id=1234").then(function(response){//处理成功的情况,走then里面的函数console.log(response);}).catch(function(error){//处理错误的情况,走catch里面的函数console.log(error);}).then(function(){//总会执行这里面的函数});

当参数比较多时,可以使用params传入。

    axios.get("/user",{params:{id:12345}}).then(function(response){//处理成功的情况,走then里面的函数console.log(response);}).catch(function(error){//处理错误的情况,走catch里面的函数console.log(error);}).then(function(){//总会执行这里面的函数});

但因为回调函数的作用域改变,如果想要在axios里面使用this指针,会报错undefinded,所以更经常的是如下箭头函数的形式,使得回调函数的作用域和其父级相同。

axios.get("/user/findAll").then((response)=> {console.log(response);}).catch(function (error) {console.log(error);}).then(function () {console.log("请求成功发送");});

post请求

axios会自动把请求体里的数据,在这里即username和password字段,转成json后传给后端。

axios.post("/user",{username: 'shanshan',password: '12345'}).then(function(response){//处理成功的情况,走then里面的函数console.log(response);}).catch(function(error){//处理错误的情况,走catch里面的函数console.log(error);}).then(function(){//总会执行这里面的函数});

支持async/await用法

    async function getUser() {try {const response = await axios.get('user?id=12345');console.log(response);} catch (error) {console.error(error);}}

跨域问题

同源策略与CORS

  • 同源策略:为了保证浏览器的安全,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源。
  • 同源:即两个页面具有相同的协议、主机、端口号。
  • 为了解决跨域问题,CORS制定了一个技术标准,使得可以在不破坏既有规则的前提下,通过后端服务器实现CORS接口,从而实现跨域通信。
  • CORS将请求分为两类:简单请求和非简单请求。

GET、POST、application/x-www-form-urlencoded、multipart/form-data、text/plain等常见的请求属于简单请求。
在后端的controller类上面加一个@CrossOrigin注解,即可使得控制器内所有请求都通过跨域问题。

Axios引入与使用

在main.js里写上

import axios from 'axios';
axios.defaults.baseURL = 'http://localhost:8088'
Vue.prototype.$http = axios

在App.vue里发送axios请求,一般在页面被挂载前就发送

export default {name: 'App',data: function () {return {movies: [{ id: 1, title: "金刚狼1", rating: 8.7 },{ id: 2, title: "金刚狼2", rating: 8.8 },]}},created: function () {this.$http.get("/user/findAll").then((response)=> {console.log(response);}).catch(function (error) {console.log(error);}).then(function () {console.log("请求成功发送");});},mounted: function () {console.log("app被挂载完毕");},components: {Movie}
}
http://www.lryc.cn/news/185066.html

相关文章:

  • 外汇天眼:真实记录,投资者在盗版MT4平台SCE Group上做交易的经历!
  • FFmpeg 命令:从入门到精通 | ffmpeg 命令视频录制
  • html 笔记:CSS
  • 【LeetCode - 每日一题】901. 股票价格跨度(23.10.07)
  • 第二证券:突发!A股T+0?刚刚,紧急回应!
  • ShardingSphereJDBC5.4.0支持Nacos配置(SpringCloud版)
  • 基于SSM的学院学生论坛系统的设计与实现
  • Unity记录5.4-地图-带种子的柏林噪声
  • 阅读论文:Label-Free Liver Tumor Segmentation
  • leetcode64 最小路径和
  • 金盘图书馆微信管理后台信息泄露漏洞 复现
  • nginx实现负载均衡(三)
  • Android---深入理解ClassLoader的加载机制
  • 超自动化加速落地,助力运营效率和用户体验显著提升|爱分析报告
  • Linux posix_spawn和fork的区别
  • 聊聊分布式架构02——Http到Https
  • 1024 画跳动的爱心#程序代码 #编程语言 #计算机
  • 【排序算法】堆排序详解与实现
  • java Spring Boot整合jwt实现token生成
  • 如何使用Git和GitHub进行版本控制
  • 彻底解决 WordPress cURL error 28 错误
  • LLM项目代码改写
  • 小谈设计模式(14)—建造者模式
  • 【kubernetes】k8s中的选主机制
  • 学生选课系统基础版
  • redis no-appendfsync-on-rewrite
  • Spring Cloud Gateway2之路由详解
  • 阿里云RDS关系型数据库详细介绍_多版本数据库说明
  • Vue中的数据绑定
  • 前后端分离计算机毕设项目之基于SpringBoot的旅游网站的设计与实现《内含源码+文档+部署教程》