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

Add axios components through vue to solve the problem of null parameters in post (detailed tutorial)

亚连
Release: 2018-06-01 11:35:39
Original
2111 people have browsed it

Below I will share with you an article about adding axios components based on vue to solve the problem of null parameters in post. It has a good reference value and I hope it will be helpful to everyone.

Okay, the goods will be shipped below.

1. Install axios

npm install axios --save
Copy after login

2. Add axios component

import axios from 'axios'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.baseURL = 'http://localhost:7878/zkview';
Vue.prototype.$ajax = axios;
Copy after login

3. Get request

testGet: function () {
 this.$ajax({
  method: 'get',
  url: '/test/greeting',
  params: {
   firstName: 'Fred',
   lastName: 'Flintstone'
  }
 }).then(function (response) {
  console.log(response);
 }).catch(function (error) {
  console.log(error);
 });
},
Copy after login

4. Post request

testPost: function () {
    var params = new URLSearchParams();
    params.append('name', 'hello jdmc你好');
    params.append('id', '2');
    this.$ajax({
     method: 'post',
     url: '/test/greeting2',
     data:params
//     data: {id: '3', name: 'abc'}
    }).then(function (response) {
     console.log(response);
    }).catch(function (error) {
     console.log(error);
    })
   }
Copy after login

5. Running result:

##6. Note:

There are two ways to pass parameters when using the post method, one is the normal method and the other is the json method. If the background accepts the normal method, then use the above method. Can.

Ordinary formed method

var params = new URLSearchParams();
params.append('name', 'hello jdmc你好');
params.append('id', '2');
data:params
Copy after login

Backend receiving parameters:

public Student greeting2(int id,String name) {
Copy after login

json method

data: {id: '3', name: 'abc'}
Copy after login

Background receiving parameters

public Object greeting2(@RequestBody Object student) {
Copy after login
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Let’s talk about the use of JS animation library Velocity.js

vue filter filter instance detailed explanation

Vue.js dynamically assigns value to img's src

The above is the detailed content of Add axios components through vue to solve the problem of null parameters in post (detailed tutorial). 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