I'm relatively new to Symfony, and I think my problem is in my controller, but cannot see it. I'm sending valid JSON via ajax request to my controller. When attempting to decode it, the resulting array is of length 0, like my JSON isn't being decoded properly or maybe returned by getContents()
properly?
js/ajax:
$('#aggregate').on('click',function(){
var sorted = [];
$('.sortable-items').each(function(){
sorted.push(JSON.stringify($(this).sortable('toArray')));
});
console.log(sorted);
$.ajax({
url: '/documentwarehouse/items/aggregate',
type: "POST",
contentType : 'application/json',
data: {"sorted": sorted},
success: function (data){
alert(data);
}, error: function(data){
alert("Sorry");
}
});
});
example JSON stored in var sorted
, and validated via JSONlint:
["[\"list1_23\",\"list1_24\",\"list1_16\",\"list1_17\",\"list1_19\"]", "[\"list2_22\"]", "[\"list4_21\"]"]
So, what gets sent as the json data via ajax, also validated, is:
{"sorted":[" . [\"list1_23\",\"list1_24\",\"list1_16\",\"list1_17\",\"list1_19\"]", "[\"list2_22\"]", "[\"list4_21\"]"]}
controller:
public function aggregateAction(Request $request){
$arrayOfListArrays = json_decode($request->getContent(),true);
$response = new JsonResponse([sizeof($arrayOfListArrays)]);
$response->send();
return $response;
}
The response alerted in the success
block of my ajax call is 0
.