Axios post adds extra key to object

心不动则不痛 提交于 2019-12-25 05:17:13

问题


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:


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.



来源:https://stackoverflow.com/questions/41974850/axios-post-adds-extra-key-to-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!