PHP LaravelでJson on Ubuntu12.04 LTS
Route::get('users', function()
{
$name = Input::get('name');
Log::info('Start Users:'.$name);
$truth = array(
'name1' => $name
);
/* case of using json_encode */
$aaa = json_encode($truth);
Log::info('truth:'.$aaa);
/* case of using Response::json() */
$response=Response::json($truth);
$response->header('Content-Type', 'application/json');
return $response;
});
# サーバーを起動。
$ php artisan serve
ブラウザで動作確認。
http://localhost:8000/users?name=hogehoge
responseは以下のとおりとなった。
{"name1":"hogehoge"}
参考:
laravel 4 why send content-type when sending json?