vue post application/x-www-form-urlencoded如何實(shí)現(xiàn)傳參
vue post application/x-www-form-urlencoded傳參
在使用axios進(jìn)行參數(shù)獲取時(shí),始終獲取不到,但是調(diào)用postman是正常的,所以初步估計(jì)是參數(shù)格式不正確,那么正確的應(yīng)該怎么寫(xiě)呢?
一般按照正常的邏輯,我們?cè)趥鬟fapplication/x-www-form-urlencoded時(shí),參數(shù)應(yīng)該這樣寫(xiě),但實(shí)際操作中發(fā)現(xiàn)一只獲取不到參數(shù)。
axios.create({ baseURL: 'url', timeout: 10000, headers: { 'Content-Type': 'application/json' } }).post('xxx/xxx/xxx',JSON.stringify({ name:'', age:12 }),{ headers:{ 'Content-Type': 'application/x-www-form-urlencoded' } }).then(function(response){ console.log(JSON.stringify(response)); }).catch(function(error){ console.log(error); });
只需要添加兩句代碼,就可以正常獲取啦.
var qs = require('qs');
然后把JSON.strinify改為qs.stringify就可以了。
var qs = require('qs'); axios.create({ baseURL: 'url', timeout: 10000, headers: { 'Content-Type': 'application/json' } }).post('xxx/xxx/xxx',qs.stringify({ name:'', age:12 }),{ headers:{ 'Content-Type': 'application/x-www-form-urlencoded' } }).then(function(response){ console.log(JSON.stringify(response)); }).catch(function(error){ console.log(error); });
用VUE"application/x-www-form-urlencoded"傳值問(wèn)題
這邊我用的是VUE 描述"application/x-www-form-urlencoded"傳值問(wèn)題
var qs = require('qs') let param = { 'ids':id }; let token = window.localStorage.getItem("token"); axios .post(api.purchase, qs.stringify(param), { headers: { "Content-Type": "application/x-www-form-urlencoded", Authorization: token } }) .then(res => { console.log(res.data); // Toast("程序異常,請(qǐng)稍后再試"); }); },
把JSON.strinify改為qs.stringify就可以了
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決el-date-picker 寬度溢出瀏覽器的問(wèn)題
這篇文章主要介紹了解決如何el-date-picker 寬度溢出瀏覽器問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-07-07vue打包后的線上部署Apache、nginx全過(guò)程
這篇文章主要介紹了vue打包后的線上部署Apache、nginx全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02vue+elementUI實(shí)現(xiàn)簡(jiǎn)單日歷功能
這篇文章主要為大家詳細(xì)介紹了vue+elementUI實(shí)現(xiàn)簡(jiǎn)單日歷功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09利用Vue3實(shí)現(xiàn)可復(fù)制表格的方法詳解
表格是前端非常常用的一個(gè)控件,本文主要為大家介紹了Vue3如何實(shí)現(xiàn)一個(gè)簡(jiǎn)易的可以復(fù)制的表格,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-12-12給vue項(xiàng)目添加ESLint的詳細(xì)步驟
本篇文章主要介紹了給vue項(xiàng)目添加ESLint的詳細(xì)步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09