PHP - Laravel 接口请求返回 JSON 数据

496 阅读1分钟
  • 在请求接口中返回 JSON 数据

    Route::get('req', function () {
        return ['id'=>'0', 'name'=>'dzm'];
    });
    
    Route::get('req', function () {
        return response()->json(['id'=>'0', 'name'=>'dzm']);
    });
    

  • 修改接口返回状态,之前返回的是 200,有时候可能请求的错误,需要返回对应的错误状态码。

    Route::get('req', function () {
        // 参数1 返回参数
        // 参数2 HTTP状态码,默认为200
        // return response()->json(['id'=>'0', 'name'=>'dzm'], 201);
    });