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中世界最好的语言
Release: 2018-04-12 11:37:40
Original
5621 people have browsed it

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
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
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
Copy after login

Background receiving parameters:

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

json way

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

Backend receiving parameters

public Object greeting2(@RequestBody Object student) {
Copy after login
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!

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