Vue利用axios發(fā)送請(qǐng)求并代理請(qǐng)求的實(shí)現(xiàn)代碼
由于瀏覽器的同源策略,發(fā)送請(qǐng)求時(shí)常常遇到跨域問題,一種解決辦法是讓后端配置跨域,還有一種就是使用代理(與前端工程一起啟動(dòng),同一個(gè)端口),因?yàn)?strong>代理不是通過瀏覽器發(fā)送的,所以不受同源策略的限制
服務(wù)器代碼
用SpringBoot工程(端口為8082)簡(jiǎn)單寫了一個(gè)Controller層,用于·接收前端發(fā)來的請(qǐng)求,并返回?cái)?shù)據(jù),前端請(qǐng)求
http://localhost:8082/students 時(shí)會(huì)返回學(xué)生列表數(shù)據(jù),訪問 http://localhost:8082/cars 時(shí)會(huì)返回汽車列表數(shù)據(jù)
import com.example.pojo.Car; import com.example.pojo.Student; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class TestController { private List<Student>studentList=List.of( Student.builder().id("001").name("張三").age(Short.valueOf("18")).build(), Student.builder().id("002").name("李四").age(Short.valueOf("19")).build(), Student.builder().id("003").name("王五").age(Short.valueOf("20")).build() ); private List<Car>carList=List.of( Car.builder().id("001").name("奧迪").price(100000F).build(), Car.builder().id("002").name("瑪莎拉蒂").price(500000F).build(), Car.builder().id("003").name("奔馳").price(300000F).build() ); @GetMapping("/students") public List<Student>getStudentList(){ return studentList; } @GetMapping("/cars") public List<Car>getCarList(){ return carList; } }
前端代碼
通過配置代理進(jìn)行請(qǐng)求的轉(zhuǎn)發(fā),實(shí)現(xiàn)跨域訪問,在vue.config.js文件中配置如下代碼
const { defineConfig } = require('@vue/cli-service') module.exports = defineConfig({ transpileDependencies: true, //開啟代理服務(wù)器 devServer:{ proxy: { '/api': { target: 'http://localhost:8082/', // 你想要代理到的地址 pathRewrite: { '^/api': '/' // 重寫路徑,將 /api 替換為 / } } } } })
通過axios發(fā)送請(qǐng)求,先在終端輸入 npm i axios 引入axios的依賴包,然后通過一下代碼發(fā)送請(qǐng)求
axios.get("/api/students") .then( //請(qǐng)求成功的回調(diào)函數(shù) response=>{ this.studentList=response.data }, //請(qǐng)求失敗的回調(diào)函數(shù) err=>{ alert(err.message) } )
到此這篇關(guān)于Vue利用axios發(fā)送請(qǐng)求并代理請(qǐng)求的文章就介紹到這了,更多相關(guān)Vue axios發(fā)送請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue使用axios發(fā)送請(qǐng)求簡(jiǎn)單實(shí)現(xiàn)代碼
- Vue axios庫避免重復(fù)發(fā)送請(qǐng)求的示例介紹
- Vue?axios庫發(fā)送請(qǐng)求的示例介紹
- Vue使用axios發(fā)送請(qǐng)求并實(shí)現(xiàn)簡(jiǎn)單封裝的示例詳解
- vue項(xiàng)目使用axios發(fā)送請(qǐng)求讓ajax請(qǐng)求頭部攜帶cookie的方法
- vue中axios處理http發(fā)送請(qǐng)求的示例(Post和get)
- Vue.js實(shí)戰(zhàn)之使用Vuex + axios發(fā)送請(qǐng)求詳解
- vue-cli3.0 axios跨域請(qǐng)求代理配置方式及端口修改
- Vue3.0?axios跨域請(qǐng)求代理服務(wù)器配置方式
相關(guān)文章
vue如何監(jiān)聽頁面的滾動(dòng)的開始和結(jié)束
這篇文章主要介紹了vue如何監(jiān)聽頁面的滾動(dòng)的開始和結(jié)束,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07element?tab標(biāo)簽管理路由頁面的項(xiàng)目實(shí)踐
本文主要介紹了element?tab標(biāo)簽管理路由頁面的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08vue內(nèi)置組件component--通過is屬性動(dòng)態(tài)渲染組件操作
這篇文章主要介紹了vue內(nèi)置組件component--通過is屬性動(dòng)態(tài)渲染組件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07創(chuàng)建項(xiàng)目及包管理yarn create vite源碼學(xué)習(xí)
這篇文章主要為大家介紹了創(chuàng)建項(xiàng)目及包管理yarn create vite源碼學(xué)習(xí)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09