使用 Axios 发布数据 [英] posting data with Axios

查看:27
本文介绍了使用 Axios 发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用这样的代码:

I need to use a code like this:

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

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

所以,它将与执行相同:

so, it will be the same as executing:

axios({
  method: 'post',
  url: '/user/12345',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  }
});

这可以使用 Java Script 6 吗?

Is this possible using Java Script 6?

推荐答案

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

You can create your own object and pass it to your data request like this:

var obj = {
  [myKey]: value,
}

or 

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

使用动态键创建对象

将变量名值对动态添加到 JSON 对象

如何发布请求

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

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

这篇关于使用 Axios 发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆