I am trying to send a JSON object into an Symfony action.
The script used to build and send the array looks like this:
var rows, arr = [];
rows = table.find('table tbody tr');
arr['x'] = new Array(rows.length);
table.find('table tbody tr').each(function (i) {
arr['x'][$(this).find('.username').text() - 1] = i;
});
console.log(arr); //PRINTS THE PROPER JSON OBJECT
$.ajax('some/correct/path', {
type: 'PUT',
data: arr,
datatype: 'json',
success: function (e) {
console.log(e);
}
});
And here I have an action for which should for now do nothing but capture the request data and send it back:
/**
* @View\Route("/x", name="x", options={"expose"=true})
* @View\Method("PUT")
*/
public function xAction(Request $request)
{
$all = $request->request->all();
return $this->json($all);
}
The problem here is that when I use the console.log on the arr
variable, it results in a proper print, yet when I console log the e
which was supposed to come from the action the result is:
[]
Here are the things I have already tried:
- Running the
$.ajax
withdata : 'sometext'
(response was:{sometext: ""}
) - Changing the method to POST
- Removing the
datatype: 'json'
- Setting the data to:
JSON.stringify(arr)
- Setting the data to:
{data : arr}
- Setting the data to:
{data : JSON.stringify(arr)}
- Setting the arr values directly into the array (without the
['x']
key) - Clearing the cache
- Installing Linux
- Setting laptop on fire
- Going back in time to kill hitler
So far I have ran out of ideas to have it work properly. What could be the issue here and how can I make it work?
1 Answer 1
arr
should be an object not array. Arrays only have numeric index and when you add the property "x"
it becomes an "array like" object
Change
arr = [];
To
arr = {};
data: JSON.stringify(arr), contentType: "application/json"
?