Tuesday 23 May 2017

Read complex objects posted with x-www-form-urlencoded

The client is posting data to my Node/Express app using x-www-form-urlencoded for Content-Type.

I am using body-parser, and the data for the following incoming request can be read using req.body.id, req.body.name :

var item = { id: 123, name: "Abc" };
$.ajax(
 { ...,
   data: item,
   ...
 })

However, when the request is as follows:

$.ajax(
 { ...,
   data: { A: item1, B: item2 },
   ...
 })

where item1 and item2 are of the same type as the previous item, req.body now does not contain any parameterized properties. How can I read the data in this situation?

I can see the data coming in like A[id]=123&A[name]=Abc&B[id]=123&B[name]=....



via Old Geezer

No comments:

Post a Comment