vue开发过程中,总会碰到一些问题,当然任何问题都不能阻止我们前进的脚步,话不多说,下面是我在开发过程中请求参数所碰到的问题
1. 在暂时没有后台数据的时候,post请求的参数大多会以 name:a,age:b 的格式去写
import axios from 'axios';
axios.post(url,
{name:'0',age:''},
{emulateJSON: true},
{ // 这里是跨域写法
headers:{"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",} // 这里是跨域的写法
}
).then(
reponse=>{
console.log(reponse);
this.tableData=reponse.data.data
}
);
总结:这样写法是没有问题的,
2. 若是后台已经写好,但post的请求要以 name:a&age:b 的方式去写的话,上面你的写法就会请求不到数据,这时我们就要使用一个插件来解决这个问题
2.1 安装qs
- npm install --save axios vue-axios qs**
2.2 在请求的页面加入
import qs from 'qs';
import axios from 'axios';
axios.post(
url,qs.stringify(
{
// 通过qs.stringify()将对象解析成URL的形式
name:'0', age:'2'
}
),
{emulateJSON: true},
{
headers:{"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",}
},
).then(
response=>{
console.log(reponse);
this.tableData=reponse.data.data
}
)
总结:以上所述是给大家介绍的vue中post请求以a=a&b=b 的格式写遇到的问题