Currently, I am working on creating an API using Ruby on Rails and facing a challenge with sending an array through ajax.
The task seems straightforward, but the issue arises when the array is received as an associative array instead of a regular one!
Essentially, the array with objects like:
[
{
"shipping_id":"1",
"option":"1"
},
{
"shipping_id":"2",
"option":"2"
}
]
is transformed into:
{"0"=>{"shipment_id"=>"1", "option"=>"1"}, "1"=>{"shipment_id"=>"2", "option"=>"2"}}
rather than
[{"shipping_id"=>"1", "option"=>"1"}, {"shipping_id"=>"2", "option"=>"2"}]
Below is the JavaScript code I am utilizing to test the API:
function select_shipping(){
obj1 = {
"shipment_id": "1",
"option": "1"
};
obj2 = {
"shipment_id": "2",
"option": "2"
};
var shipments = [obj1, obj2];
var payload = {
user_options: shipments
}
$.post('/shipping/calculate/select', // url
payload, // data to be submit
function(data, status, jqXHR) {// success callback
console.log(data);
})
}
I am looking for a solution to transform my payload into a regular array format instead of an associative array. Any suggestions?