【问题标题】:Can't parse json response from laravel无法解析来自 laravel 的 json 响应
【发布时间】:2016-08-14 08:38:03
【问题描述】:

标题存在这样的问题。在 PHP 中:

return response()->json([ 'somedata' => 1 ]);

在 JS 中:

$.get('/page', function(data) { console.log(data) });

结果:

HTTP/1.0 200 OK
Cache-Control: no-cache
Content-Type:  application/json

{"somedata":"1"}

是的,它是纯文本。为什么响应包含标题?当然,我无法解析。

【问题讨论】:

    标签: json ajax laravel server http-headers


    【解决方案1】:

    问题是——php7中的返回类型。

    public function index() : string {
        return response()->json([ 'somedata' => 1 ]);
    }
    

    没有: string一切正常!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多