Home >Web Front-end >JS Tutorial >How to deal with null parameters passed in post in Vue

How to deal with null parameters passed in post in Vue

php中世界最好的语言
php中世界最好的语言Original
2018-04-12 11:37:405616browse

This time I will show you how to handle null parameters in post in vue. What are the things to pay attention to when handling null parameters in post in vue? The following is a practical case. Get up and take a look. Okay, the goods will be shipped below.

1, Installationaxios

npm install axios --save

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;

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);
 });
},

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);
    })
   }
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.

Ordinary formed way

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

Background receiving parameters:

public Student greeting2(int id,String name) {

json way

data: {id: '3', name: 'abc'}

Backend receiving parameters

public Object greeting2(@RequestBody Object student) {
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

springmvc cannot accept parameters when axios sends a request


How does vue axios interrupt the request when switching pages? accomplish


The above is the detailed content of How to deal with null parameters passed in post in Vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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