vue post application/x-www-form-urlencoded如何實現(xiàn)傳參
vue post application/x-www-form-urlencoded傳參
在使用axios進行參數(shù)獲取時,始終獲取不到,但是調(diào)用postman是正常的,所以初步估計是參數(shù)格式不正確,那么正確的應(yīng)該怎么寫呢?
一般按照正常的邏輯,我們在傳遞application/x-www-form-urlencoded時,參數(shù)應(yīng)該這樣寫,但實際操作中發(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"傳值問題
這邊我用的是VUE 描述"application/x-www-form-urlencoded"傳值問題
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("程序異常,請稍后再試"); }); },
把JSON.strinify改為qs.stringify就可以了
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。