【问题标题】:How to read JsonResponse in php如何在 php 中读取 JsonResponse
【发布时间】:2016-07-16 15:26:19
【问题描述】:

如何从这里获取 id

JsonResponse {#457 ▼
  #data: "{"bill":{"id":11,"invoice_no":"9m36r9_1459170388239"}}"
  #callback: null
}

我从这个 laravel 代码中得到这个输出

return Response::json($response);

我尝试了json_decode,但在这里不起作用,即将出现空白输出。

感谢您的帮助。

【问题讨论】:

  • 你对json_decode()做了什么尝试?
  • 你想在哪里显示这些数据?
  • 我可以通过 ajax 获取 js 中的数据,但这里我试图进入 laravel/php 生成 pdf
  • 先解码,然后返回一个数组,这样你就可以访问了。
  • 其他代码在哪里?

标签: php json laravel laravel-5 laravel-5.2


【解决方案1】:

这对我有用,使用 getData()。 Laravel 5.7.

$jsonResponse = Response::json([
    'id' => 1,
    'test' => 'test'
]);
$content = $jsonResponse->getData();
$id = $content->id;

【讨论】:

    【解决方案2】:

    这样试试

    $jsonResponse = Response::json([
            'id' => 1,
            'test' => 'test'
        ]);
    
    $content = $jsonResponse->getContent();
    $array = json_decode($content, true);
    $id = $array['id']
    

    【讨论】:

    • 感谢 getContent()
    【解决方案3】:

    试试这个,它对我有用:

    $.ajax({
       type: "POST",
       url: url,
       data: {
          ...
       },
       success: function (data) {
           alert(data.bill.id)       
       },
       error: function (err) {
    
       }
    
    });
    

    如果这不起作用,请告诉我。

    【讨论】:

    • 你确定它有效吗?直到你添加dataType:'json'或成功使用JSON.parse;
    • 我确定,顺便说一句,这是我的回复return response()->json($data);
    猜你喜欢
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 2011-04-23
    • 2023-04-03
    • 2016-08-15
    • 2016-04-03
    相关资源
    最近更新 更多