I'm making post requests using ajax in my symfony 4.4 project, and i return the response using fosrestbundle, sometimes it returns a correct json response but sometimes, the response includes also the data sent with request as a string, which is strange, and i don't know why.
This is my ajax request (** i'm using jsrouting-bundle to embed my routes into javascript):
var url = Routing.generate('orders_change_status', {id: order.id}, true); $.ajax({ type: "PUT", url: url, data: data, success: function (data) { }, error: function (xhr, status, error) { } });
And this is my response :
/** * @Route("/orders/change_status/{id}", name="orders_change_status", methods={"PUT"}) */public function changeStatus(Request $request, $id){ $status = $request->request->get("status"); if (isset($id) && isset($status)) { $data = $request->request->all(); $apiResponse = $this->apiHelper->changeOrderStatus($id, $data); $jsonData = json_decode($apiResponse->getBody()->getContents()); $checked = $this->hasErrors($apiResponse, $jsonData); if ($checked) { $flash = $request->request->get("flash"); if (isset($flash) && $flash == 1) $this->addFlash('order_success', $this->translator->trans("The order has been edited successfully")); return new JsonResponse(array("code" => Response::HTTP_OK, "test" => "test"), Response::HTTP_OK); } } return View::create(array("code" => Response::HTTP_BAD_REQUEST, "message" => "Something went wrong!"), Response::HTTP_BAD_REQUEST);}public function changeOrderStatus($id, $data){ $data['author-id'] = $this->security->getUser()->getId(); return $this->client->put($this->getApiUrl() . '/orders/' . $id . '/change_status', ['json' => $data ]);}
Somtimes the response is correct in json format :
{"code":200,"message":"test"}
But sometimes it includes the data sent as a string, i don't know why :
status=3¬e=No+Answer&flash=1&postponed_to=2022-05-17+12%3A36{"code":200,"message":"test"}
This happens only in production environment!!