简体   繁体   English

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

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

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

This is my ajax request (** i'm using jsrouting-bundle to embed my routes into javascript):这是我的 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) {
            
        }
    });

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: Somtimes json 格式的响应是正确的:

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

But sometimes it includes the data sent as a string, i don't know why:但有时它包括作为字符串发送的数据,我不知道为什么:

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

***This happens only in production environment!! ***这只发生在生产环境!!

Thanks in advance提前致谢

After hours of digging, I managed to resolve the problem by restarting all docker containers, many thanks anyway.经过数小时的挖掘,我设法通过重新启动所有 docker 容器来解决问题,无论如何非常感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM