最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - get form data from '[object Object]' - Stack Overflow

programmeradmin5浏览0评论

I am passing uploaded file and dataObject in FormData:

let data = new FormData();
data.append("file", this.state.files);
data.append("data", formData);

formData is just a Json Object eg: formData = { "a":123 }

Passing data to the node server using put request with axios ,I am able to get the file object and data in req, but in data field it's giving '[object Object]'

Don't know how to access this. I tried JSON.parse(req.body.data), but getting error

SyntaxError: Unexpected token o in JSON at position 1

I am passing uploaded file and dataObject in FormData:

let data = new FormData();
data.append("file", this.state.files);
data.append("data", formData);

formData is just a Json Object eg: formData = { "a":123 }

Passing data to the node server using put request with axios ,I am able to get the file object and data in req, but in data field it's giving '[object Object]'

Don't know how to access this. I tried JSON.parse(req.body.data), but getting error

SyntaxError: Unexpected token o in JSON at position 1

Share Improve this question edited Oct 27, 2019 at 18:35 Prabhat Mishra asked Feb 19, 2019 at 15:45 Prabhat MishraPrabhat Mishra 1,0212 gold badges15 silver badges35 bronze badges 3
  • 2 try using JSON.stringify(data) before sending it to the server – Silvio Biasiol Commented Feb 19, 2019 at 15:46
  • there's not enough context here, but it sounds like you've passed an actual JS object, which natively stringifies to that unhelpful [object Object]. You probably want to call JSON.stringify on it before you send it – Robin Zigmond Commented Feb 19, 2019 at 15:47
  • what is formData object? You probably need to do JSON.stringify(formData) – Andrey Commented Feb 19, 2019 at 15:47
Add a ment  | 

2 Answers 2

Reset to default 12

Looks like formData is an object that's cast to string [object Object] prior to being sent on the wire. Try data.append("data", JSON.stringify(formData)).

Send the object with JSON.stringify and then use JSON.parse once you receive data, to make the string object an object again.

发布评论

评论列表(0)

  1. 暂无评论