首页 > 解决方案 > Vue JS:从对象发送 axios 发布数据

问题描述

我需要将我的 JSON 设置为变量,因为我在 2 个地方使用它,即

let data = {
         "test": "hello",
         "test2": "hello2
    }
    axios.post('/route', {
       data
    })
    .then((response) => {
        this.$emit('anotherFunction', data);
    })

我得到一个400回复​​,然后当我检查网络请求时,有效负载是:"{data: {"test": "hello", "test2": "hello2"}}"这是有道理的,但是如果没有 JSON 的data一部分,我怎么能拥有它呢?

标签: vue.jsaxios

解决方案


你应该data像这样通过:

let data = {
         "test": "hello",
         "test2": "hello2
}
axios.post('/route', data)
.then((response) => {
        this.$emit('anotherFunction', data);
})

推荐阅读