This time I will bring you how to use post request (with code) in vue. What are the precautions when using post request in vue. The following is a practical case, one Get up and 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:
Nuxt.js SSR permission verification use
How to use JS to achieve the simplest cross-domain
The above is the detailed content of Using post request in vue (code attached). For more information, please follow other related articles on the PHP Chinese website!