虚位以待(AD)
虚位以待(AD)
首页 > 网页特效 > JavaScript > vue中post请求以a=a&b=b 的格式写遇到的问题

vue中post请求以a=a&b=b 的格式写遇到的问题
类别:JavaScript   作者:码皇   来源:互联网   点击:

这篇文章主要介绍了vue中post请求以a=a&b=b 的格式写遇到的问题,需要的朋友可以参考下

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(reponse=>{
    console.log(reponse) this.tableData=reponse.data.data }
    )

总结

以上所述是小编给大家介绍的vue中post请求以a=a&b=b 的格式写遇到的问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:

  • 解决vue处理axios post请求传参的问题
  • vue resource post请求时遇到的坑
  • vue中axios处理http发送请求的示例(Post和get)
  • Vue resource中的GET与POST请求的实例代码
  • 详解Vue用axios发送post请求自动set cookie
相关热词搜索: vue post 请求