1

I am trying to consume a JSON string built with the JS function JSON.stringify(objects). On my working local version, it is working, but on the server it raises the following error:

invalid argument supplied for foreach

After some investigations, the $POST table is empty. But in the browser console, the data is sent with the query.

Here is the content of the request:

rencontres:[{"id":"1","m":"","f":"0-2","e":"","p":"","status":"3","st":false,"si":false,"se":false,"sp":false,"ss":false}]

Here is the JSON string contained in $GET['rencontres'] (accessed by $request->get("rencontres")):

[{"id":"1","m":"","f":"0-2","e":"","p":"","status":"3","st":false,"si":false,"se":false,"sp":false,"ss":false}]

which seems correct.

Here some var_dump results: var_dump($renontres) gives [][]

var_dump($request->get("rencontres") gives also [][]

here is the method itself:

public function postSaveRencontre(Request $request){
    $em = $this->getDoctrine()->getManager();
    $rencontres = json_decode($request->get("rencontres"), true);
    //log struff
    foreach ($rencontres as $key => $r) {
        //blablabla
    }
    //return statement
}

and here the AJAX statement (url is correct):

$.ajax({
    type: "POST",
    url:"url",
    data: {rencontres:JSON.stringify(rencontres)},
    success:function(data){
        console.log("save performed");
    }
});

1 Answers1

0

Hi I Think that i get the same problem in my application
the cause of this problem is that ajax send data as content-type application/json on post
to Fix this poblem you have to add this code to your Action

$data = json_decode($request->getContent(), true);
$request->request->replace($data);
dump($request->request->get("rencontres"))

you have to add this ligne to all your Action that has POST
or Think to create a service to transform Your Json request

Barkati.med
  • 620
  • 5
  • 11