【问题标题】:Laravel appending extra data along with custom response in controllerLaravel 在控制器中附加额外数据以及自定义响应
【发布时间】:2019-07-07 05:02:06
【问题描述】:

我正在调用我的 laravel 控制器中的一个函数。我已经使用 laravel 的自定义响应来返回数据。但即使我没有添加一些其他数据,也会添加到我的回复中。

return Response::make(json_encode(array(
    'error' => false, 
    'message' => 'Redaction done successfully.', 
    'is_redacted' => 0)), 200, 
    array(
    'Content-Type' => 'application/json'
    )
);

以上是我添加的用于返回响应的代码。 但我收到的回复是:

    > * Found bundle for host redaction-stage-tcm.tylerhost.net: 0x55998c67ee40
    * Re-using existing connection! (#0) with host redaction-stage-tcm.tylerhost.net
    * Connected to redaction-stage-tcm.tylerhost.net (208.64.239.110) port 443 (#0)
    > POST /ocr/OCRWeb/v2/Documents('mfr_8')/OCRWeb.Scan HTTP/1.1
    Host: redaction-stage-tcm.tylerhost.net
    User-Agent: GuzzleHttp/6.3.3 curl/7.43.0 PHP/5.6.11-1ubuntu3.4
    OData-Version: 4.01
    OData-MaxVersion: 4.01
    Content-Type: application/json
    Content-Length: 2
    upload completely sent off: 2 out of 2 bytes
    < HTTP/1.1 200 
    < Cache-Control: no-transform
    < Cache-Control: no-cache
    < Cache-Control: no-store
    < Cache-Control: must-revalidate
    < Cache-Control: max-age=0
    < Expires: Thu, 01 Jan 1970 00:00:00 GMT
    < Vary: *
    < Pragma: no-cache
    < X-Clacks-Overhead: GNU Terry Pratchett
    < OData-Version: 4.01
    < Preference-Applied: 
    < Content-Type: application/json; odata.metadata=minimal
    < Content-Length: 163
    < Date: Wed, 13 Feb 2019 10:44:40 GMT
    < 
    * Connection #0 to host redaction-stage-tcm.tylerhost.net left intact
    {"error":false,"message":"Redaction done successfully.","is_redacted":0}

我在我的函数中调用了一些其他 API。此数据与此相关,但为什么它会附加到我的自定义响应中? 请帮帮我。

【问题讨论】:

  • 那你到底想返回什么?而且我不确定,只是猜测,您使用的 Response 外观一定属于其他命名空间,而不是 Laravel (Illuminate\Http\Response)。
  • 我错过了什么吗?内容看起来不错。如果您对默认标头感到困扰,请注意您使用代码附加自定义标头而不是替换默认标头。

标签: php laravel


【解决方案1】:

试试这个,

return response()->json([
   'error' => false, 
   'message' => 'Redaction done successfully.', 
   'is_redacted' => 0, 
   'Content-Type' => 'application/json'
], 200)

【讨论】:

  • 我试过这个但没有用。即使我发送空对象仍然会附加该数据:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-14
  • 1970-01-01
  • 2016-11-06
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 2021-05-26
相关资源
最近更新 更多