Home > Web Front-end > JS Tutorial > Using post request in vue (code attached)

Using post request in vue (code attached)

php中世界最好的语言
Release: 2018-06-04 14:59:21
Original
14681 people have browsed it

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
    })
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:

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!

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