vue中post請(qǐng)求報(bào)400的解決方案
vue post請(qǐng)求報(bào)400
1、為默認(rèn)數(shù)據(jù)格式為json,發(fā)請(qǐng)求時(shí)參數(shù)報(bào)錯(cuò)
通過(guò)以下方式修改數(shù)據(jù)格式即可
import qs from 'qs'; // import qs from 'querystring' const data = { 'bar': 123 }; const options = { ? method: 'POST', ? headers: { 'content-type': 'application/x-www-form-urlencoded' }, ? data: qs.stringify(data), ? url, }; axios(options);
2、檢查發(fā)送的數(shù)據(jù)格式是否與后端要求相匹配,要求字符串,發(fā)送了數(shù)組也可能會(huì)出現(xiàn)400錯(cuò)誤
vue 異步請(qǐng)求問(wèn)題
Vue 中使用 axios 進(jìn)行網(wǎng)絡(luò)請(qǐng)求,使用起來(lái)和 jQuery 中的 $.get 和 $.post 功能類似。
安裝依賴:cnpm install --save axios
1. get 請(qǐng)求方式
給定服務(wù)器控制器代碼,可以接收 name 和 age 參數(shù),設(shè)置到 Map 中并返回。
注意:控制器上有跨域注解。前后端分離都是跨域請(qǐng)求。且端口不能設(shè)置 8080 端口,避免端口沖突。
<template> <div id="app"> <div>用戶名:{{stu.name}}</div> <div>年齡:{{stu.age}}</div> </div> </template> <script> import axios from "axios" export default { name: 'App', data(){ return{ stu:{ "name":null, "age":null, } } }, mounted(){ //頁(yè)面加載事件 axios.get("http://localhost:8181/index?name=zhangsan&age=18") .then(res => { console.log(res.data); this.stu = res.data; }) .catch(error => { console.log(error); }) } } </script>
2. post 請(qǐng)求方式
POST 也支持 URL 重寫方式傳參,即通過(guò) ? 和 & 傳遞參數(shù),如果使用這種方式傳參必須要結(jié)合 querystring 使用。
<template> <div id="app"> 發(fā)起請(qǐng)求獲取到的結(jié)果:<span>{{name}},{{age}}</span> </div> <router-view/> </template> <script> import axios from "axios" import qstring from "querystring" export default { name: 'App', data(){ return{ name:null, age: null, } }, mounted(){ // 頁(yè)面加載事件 axios.post("http://localhost:8181/index", qstring.stringify({ name : "張三", age: 23 })) //處理的結(jié)果是 name=張三&age=23 .then(res => { console.log(res.data); this.name = res.data.name; this.age = res.data.age; }) .catch(error => { console.log(error); }) } } </script>
后端的業(yè)務(wù)代碼:
@RestController @CrossOrigin //跨域注解 public class TestController { @RequestMapping("/index") public Map<String, Object> index(@Param("name") String name,@Param("age") Integer age){ Map<String, Object> result = new HashMap<>(); result.put("name", name); result.put("age", age); return result; } }
3. axios 全局設(shè)置
如果使用上面的這種方式,需要在每個(gè)頁(yè)面中都導(dǎo)入 axios,更簡(jiǎn)便的方式是全局綁定 axios。
修改 main.js
import { createApp } from 'vue' import App from './App.vue' //導(dǎo)入./router/index文件里面配置好的路由 import router from './router/index' import axios from "axios" import qstring from "querystring" //全局Vue對(duì)象 const Vue=createApp(App); // 設(shè)置axios、qstring全局 Vue.config.globalProperties.$axios=axios; Vue.config.globalProperties.$qstring=qstring; Vue.use(router).mount('#app');
頁(yè)面中的寫法:在任何頁(yè)面中都可以直接使用 this.$axios 和 this.$qstring 進(jìn)行設(shè)置。
<template> <div id="app"> 發(fā)起請(qǐng)求獲取到的結(jié)果:<span>{{name}},{{age}}</span> </div> <router-view/> </template> <script> export default { name: 'App', data(){ return{ name:null, age: null, } }, mounted(){ // 頁(yè)面加載事件 this.$axios.post("/api/index", this.$qstring.stringify({ name: "張三", age: 15 })) .then(res => { console.log(res.data); this.name = res.data.name; this.age = res.data.age; }) .catch(error => { console.log(error); }) } } </script>
4. 請(qǐng)求代理
在 Vue 中發(fā)起網(wǎng)絡(luò)請(qǐng)求時(shí) URL 都使用的是完整的 URL,可以把公共 URL 提出來(lái),提出后發(fā)起網(wǎng)絡(luò)請(qǐng)求時(shí),URL 只寫路徑部分,省略協(xié)議、IP 和端口。
如果沒(méi)有請(qǐng)求代理,每次在瀏覽器開(kāi)發(fā)者工具看見(jiàn)真實(shí)請(qǐng)求服務(wù)器地址,這樣話就把服務(wù)器暴露給客戶端了。使用代理后只能看見(jiàn)代理前請(qǐng)求,保護(hù)真實(shí)服務(wù)器地址。
在項(xiàng)目根路徑(不是src)下新建 vue.config.js:
這個(gè)配置文件操作完成后必須重啟
const { defineConfig } = require('@vue/cli-service'); module.exports = defineConfig({ transpileDependencies: true, lintOnSave:false, //配置請(qǐng)求代理 devServer: { proxy: { //當(dāng)請(qǐng)求Vue項(xiàng)目路徑都以/api開(kāi)頭時(shí),轉(zhuǎn)發(fā)給下面 '/api': { //服務(wù)器URL target: "http://localhost:8181/", ws: true, pathRewrite: { //把路徑中的api去掉 '^/api': '' }, changeOrigin: true } } } });
發(fā)起請(qǐng)求時(shí)可以使用 /api/xxx 的形式:
mounted(){ // //頁(yè)面加載事件 axios.get("/api/index?name=zhangsan&age=18") .then(res => { console.log(res.data); this.name = res.data.name; this.age = res.data.age; }) .catch(error => { console.log(error); }) }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue組件傳值過(guò)程中丟失數(shù)據(jù)的分析與解決方案
這篇文章主要給大家介紹了關(guān)于Vue組件傳值過(guò)程中丟失數(shù)據(jù)的分析與解決方案,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03vue3中安裝使用vue-i18n實(shí)時(shí)切換語(yǔ)言且不用刷新
這篇文章主要介紹了vue3中安裝使用vue-i18n實(shí)時(shí)切換語(yǔ)言不用刷新問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04vue+bpmn.js實(shí)現(xiàn)自定義流程圖的完整代碼
這篇文章主要介紹了vue+bpmn.js實(shí)現(xiàn)自定義流程圖的完整代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借價(jià)值,需要的朋友參考下吧2024-03-03vue組件中傳值EventBus的使用及注意事項(xiàng)說(shuō)明
這篇文章主要介紹了vue組件中傳值EventBus的使用及注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11vue項(xiàng)目中axios請(qǐng)求網(wǎng)絡(luò)接口封裝的示例代碼
這篇文章主要介紹了vue項(xiàng)目中axios請(qǐng)求網(wǎng)絡(luò)接口封裝的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12Vue3不支持Filters過(guò)濾器的問(wèn)題
這篇文章主要介紹了Vue3不支持Filters過(guò)濾器的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Vue做一個(gè)簡(jiǎn)單的隨機(jī)點(diǎn)名冊(cè)
這篇文章主要介紹的是如何用Vue做一個(gè)簡(jiǎn)單的隨機(jī)點(diǎn)名冊(cè),主要是做個(gè)簡(jiǎn)單的點(diǎn)名器,不做樣式,需要的朋友可以參考一下,希望對(duì)你有所幫助2021-12-12