使用Axios发布数据

时间:2017-04-18 23:49:07

标签: javascript post axios

我需要使用这样的代码:

td {
    background: blue;
}

td:nth-child(4n+1), td:nth-child(4n+2) {
    background: red;
}

所以,它与执行相同:

vr1 = 'firstName'
value1 = 'Fred'
vr2 = 'lastName'
value2 = 'Flinstone'

axios({
  method: 'post',
  url: '/user/12345',
  data: {
     vr1: Value1,
     vr2: Value2
  }
});

这可以使用Java Script 6吗?

3 个答案:

答案 0 :(得分:9)

也请尝试使用自己的主机名

baseURL

import axios from 'axios'

let var1 = 'firstName'
let value1 = 'Fred'
let var2 = 'lastName'
let value2 = 'Flinstone'

const api = axios.create({baseURL: 'http://example.com'})
api.post('/user/12345', {
    var1: value1,
    var2: value2
})
.then(res => {
     console.log(res)
})
.catch(error => {
     console.log(error)
})

答案 1 :(得分:3)

您可以创建自己的对象并将其传递给您的数据请求,如下所示:

var obj = {
  [myKey]: value,
}

or 

var obj = {};
obj['name'] = value;
obj['anotherName'] = anotherValue;

Creating object with dynamic keys

Dynamically Add Variable Name Value Pairs to JSON Object

编辑:如何发布请求

const profile = {};
//...fill your object like this for example
profile[key] = value;

axios.post('profile/student', profile)
  .then(res => {
    return res;
  });

答案 2 :(得分:2)

尝试一下对我有用

const obj = {
  firstName: Fred,
  lastName: Flinstone
}
axios
  .post(
    "url",
    this.obj,
  )
  .then(response => {
    console.log(response)
  })
  .catch(error => {
    console.log(error);
  });