Home > Article > Web Front-end > How to use vue to implement post request in a=a&b=b format
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 })
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
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 })
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!