I am having trouble sending a data object for a put request in jQuery’s ajax method. I want my data to be formatted like the following example:
var data_obj = {user_id: 1, music_albums: [{title: "X", artist: "A"}, {title: "Y", artist: "B"}]};
My ajax request has the usual syntax:
$.ajax({
url: "/someurl",
type: "put",
data: data_obj,
success: function(json) { /* etc. */ }
});
I want the array of objects to be accessible on the server side to process in my API. I am using expressJS for the backend. Right now when I send over the data object, the array of objects is not preserved.
Based on previous answers to similar questions, I have also tried to call JSON.stringify() on the array property, so for example:
var data_obj = {user_id: 1, music_albums: JSON.stringify([{title: "X", artist: "A"}, {title: "Y", artist: “B”}])};
But that did not work either.
How should I format this data properly to access the music_albums
property as an array on the server side?