Axios post adds extra key to object

2019-09-16 08:53发布

I try to post an object with axios. I need it to be in a format like this:

var dataObj = {username:"username",password:"password",data1:"data1"};

When i POST it with axios and catch in the backend, it adds an extra key to the object like this:

{dataObj:{username:"username",password:"password",data1:"data1"}};

How can i get rid of this extra field before sending it to backend so it will look like this?

{username:"username",password:"password",data1:"data1"}

I know i can parse it in the backend but i cant modify the backend cuz its not mine.

This is how my axios post looks like:

axios.post('http://192.168.1.1xx:3000/data', {
    dataObj
})
.then(function (response) {
    console.log(response);
})
.catch(function (error) {
    console.log(error);
});

Thanks :)

1条回答
forever°为你锁心
2楼-- · 2019-09-16 09:55
axios.post('http://192.168.1.1xx:3000/data', 
    dataObj
)

Instead of :

axios.post('http://192.168.1.1xx:3000/data', {
    dataObj
})

Remove brackets ↪ i mean ,dataObj) instead of , {dataObj}) ... because "dataObj" is already a literal object.

查看更多
登录 后发表回答