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就可以了
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论