Home > Web Front-end > JS Tutorial > body text

How to use vue to implement post request in a=a&b=b format

php中世界最好的语言
Release: 2018-06-01 17:36:20
Original
1363 people have browsed it

This time I will show you how to use vue to implement a=a&b=b format post request, and how to use vue to implement post request in a=a&b=b formatNotesWhat are they? The following is a practical case. Let’s take a look.

During the vue development process, we will always encounter some problems. Of course, no problem can stop us from moving forward. Without further ado, here are the request parameters that I used during the development process. Problems encountered

1. When there is no background data for the time being, most of the parameters of the post request will be written in the format of 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
    })
Copy after login

There is no problem with this way of writing Yes,

2, if the background has been written, but the post request needs to be written in the form of name:a&age:b , your writing method above will not be able to request data. At this time, we have to use A plug-in to solve this problem

2.1, Install qs

 npm install --save axios vue-axios qs
Copy after login

2.2, add

    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
    })
Copy after login

to the requested page. I believe you have read the case in this article. After mastering the method, please pay attention to other related articles on the php Chinese website for more exciting content!

Recommended reading:

How to use React native ListView to add top pull-down refresh and bottom click refresh

How to use Vue2. 0Call the camera to take pictures

The above is the detailed content of How to use vue to implement post request in a=a&b=b format. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template