使用Laravel生成JSON的特殊字符

时间:2018-10-24 14:13:21

标签: php json laravel

我正在尝试在layers中创建一个api。但是,当它生成layers = {} def register_layer(cls): if cls.__name__ in layers: raise ValueError('Cannot have two layers with the same name') layers[cls.__name__] = cls return cls 时,它会包含以下特殊字符:

image 我试图什么也不返回,它仍然返回那些字符。我尝试将其放入所有可能的编纂中,并且始终保持不变。

路由在 api.php 上定义:

laravel

json函数是这样的:

Route::get('/draws',[
    'middleware' => 'cors',
    'uses' => 'DrawController@getDraws'
]);

我班的Cors是这样的:

Controller

dd($ response)向我返回此信息:

    public function getDraws(){
    $draws = Draw::all();
    $response = [
        'draws' => $draws
    ];

    $headers = ['Content-Type' => 'application/json; charset=UTF-8'];

    return response()->json($response, 200, $headers);
}

1 个答案:

答案 0 :(得分:1)

没有必要这样做:

$draws = Draw::all();
$response = [
    'draws' => $draws
];

$headers = ['Content-Type' => 'application/json; charset=UTF-8'];

return response()->json($response, 200, $headers);

您可以简单地返回集合,该集合将自动转换为正确的json响应,尝试进行如下操作:

return Draw::all()