0

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.

ginc
  • 1
  • 2
  • You have problem to fetch data from query So i would suggest you that you must use `$request->getRequest()->request->all()['sorted']` instead `$request->getContent()` – Muneer Alam May 03 '18 at 07:23
  • here is the proper explanation (https://stackoverflow.com/questions/11227285/getting-all-request-parameters-in-symfony-2) – Muneer Alam May 03 '18 at 07:24
  • Is there reason `$request->get('sorted')` or `$request->request->get('sorted')` won't work? I will give your suggestion a try. Thank you! – ginc May 03 '18 at 14:10
  • Also that SO question is about Symfony2. I'm on 3 and I think things are different; certainly, I shouldn't be using `getRequest()` for Symfony 3 – ginc May 03 '18 at 18:56

0 Answers0