簡體   English   中英

JsonResponse返回json response加上字符串格式的請求數據問題

[英]JsonResponse returns the json response plus the request data in string format problem

我在我的 symfony 4.4 項目中使用 ajax 發出發布請求,我使用 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
    ]);
}

Somtimes json 格式的響應是正確的:

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

但有時它包括作為字符串發送的數據,我不知道為什么:

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

***這只發生在生產環境!!

提前致謝

經過數小時的挖掘,我設法通過重新啟動所有 docker 容器來解決問題,無論如何非常感謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM