vue post application/x-www-form-urlencoded如何实现传参
作者:心若向阳无谓悲伤
这篇文章主要介绍了vue post application/x-www-form-urlencoded如何实现传参问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
vue post application/x-www-form-urlencoded传参
在使用axios进行参数获取时,始终获取不到,但是调用postman是正常的,所以初步估计是参数格式不正确,那么正确的应该怎么写呢?
一般按照正常的逻辑,我们在传递application/x-www-form-urlencoded时,参数应该这样写,但实际操作中发现一只获取不到参数。
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就可以了
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
