如何在PHP或Laravel中访问JsonResponse对象中的属性值?

Hen*_*Dev 3 php laravel laravel-5 laravel-5.1

我正在使用Ajax进行POST,而我的服务器正在获取数据.但是,我很难访问用户发送的值.简单来说,如何访问"user"(tom)的值?任何人都可以让我走上正轨.先感谢您.这是我的JsonResponse对象:

[2016-10-22 05:10:49] local.INFO: From Ajax: Illuminate\Http\JsonResponse Object
(
[data:protected] => {"user":"Tom","_token":"uRZJBVHH3worhjX4Ul6WlnJC1JYh3EVMNWob7Azr"}
[callback:protected] => 
[encodingOptions:protected] => 0
[headers] => Symfony\Component\HttpFoundation\ResponseHeaderBag Object
    (
        [computedCacheControl:protected] => Array
            (
                [no-cache] => 1
            )

        [cookies:protected] => Array
            (
            )

        [headerNames:protected] => Array
            (
                [cache-control] => Cache-Control
                [content-type] => Content-Type
            )

        [headers:protected] => Array
            (
                [cache-control] => Array
                    (
                        [0] => no-cache
                    )

                [content-type] => Array
                    (
                        [0] => application/json
                    )

            )

        [cacheControl:protected] => Array
            (
            )

    )

[content:protected] =>     {"user":"Tom","_token":"uRZJBVHH3worhjX4Ul6WlnJC1JYh3EVMNWob7Azr"}
[version:protected] => 1.0
[statusCode:protected] => 200
[statusText:protected] => OK
[charset:protected] => 
)
Run Code Online (Sandbox Code Playgroud)

小智 13

使用Laravel,也可以使用照明getData()方法访问数据.

$someVar->getData();
Run Code Online (Sandbox Code Playgroud)

https://laravel.com/api/5.3/Illuminate/Http/JsonResponse.html#method_getData

  • 简单但可以救我的命 (2认同)

Hen*_*Dev 3

我解决了我的问题,我将分享它以防有人需要。所以我获取 JsonObjec 的方法是在 Routes.php 中执行此操作:

Route::post('/register', function(){
if(Request::ajax()){
    Log::info('From Ajax: ' . print_r(Response::json(Request::all()), true));
    
    return var_dump(Response::json(Request::all()));
} 
});
Run Code Online (Sandbox Code Playgroud)

但我这样做是为了实际访问用户(汤姆)的价值。

$somevar = (Request::all());
Log::info('From Ajax: ' . print_r($somevar["user"], true));
Run Code Online (Sandbox Code Playgroud)

这解决了我的问题。希望它对任何人都有帮助!