I'm experiencing issues with sending data to the server.
Here is my Ajax call:
$.ajax({
url: config.api.url,
type: config.api.method,
contentType: config.api.contentType,
dataType: config.api.dataType,
data: config.api.dataType === 'GET' ? {} : JSON.parse(tmp),
headers: config.api.headers,
success: (response) => { onSuccess(response); },
error: (error) => { onError(error); }
});
This is the data I am trying to send:
{
sort: { name: 1 }
}
// The name property is set using sort['name'] = 1; in js
However, the server received it as:
{ 'sort[name]': 1 }
In my Node.js server code, I have:
exampleData = (req, res) => {
var sort = req.body.sort;
console.log(sort); // undefined
console.log(req.body); // { ..., 'sort[name]': 1 }
}
When checking the Chrome Form Data, I noticed something unusual:
https://i.sstatic.net/KktAL.png
It seems like there is an issue with reading the object correctly. Any ideas on how to fix this?