JsonResponse 返回json响应加上字符串格式的请求数据问题

问题描述 投票:0回答:1

我在我的symfony 4.4项目中使用ajax发出post请求,并且使用fosrestbundle返回响应,有时它返回正确的json响应,但有时,响应还包括随请求作为字符串发送的数据,这很奇怪,我不知道为什么。

这是我的ajax请求(**我正在使用jsrouting-bundle将我的路由嵌入到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) {
            
        }
    });

这是我的回应:

 /**
 * @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
    ]);
}

有时 json 格式的响应是正确的:

{"code":200,"message":"test"}

但有时它包含作为字符串发送的数据,我不知道为什么:

status=3&note=No+Answer&flash=1&postponed_to=2022-05-17+12%3A36{"code":200,"message":"test"}

这只发生在生产环境!!

ajax symfony symfony4 fosrestbundle fosjsroutingbundle
1个回答
0
投票

经过几个小时的挖掘,我通过重新启动所有 docker 容器成功解决了问题,无论如何,非常感谢。

© www.soinside.com 2019 - 2024. All rights reserved.