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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40188597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:38:58  来源:igfitidea点击:

How to access a property value inside JsonResponse Object in PHP or Laravel?

phplaravellaravel-5laravel-5.1

提问by HenryDev

I'm doing a POST using Ajax and my Server is getting the data just fine. However, I'm struggling to access the value the user sent. In simple words how can I access the value of "user" (tom) ?. Anyone could get me on the right track please. Thank you in advance. Here's my JsonResponse object:

我正在使用 Ajax 进行 POST,我的服务器正在获取数据。但是,我正在努力访问用户发送的值。简而言之,我如何访问“用户”(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] => 
)

采纳答案by HenryDev

I solve my issue and I'm going to share it in case someone needs it. So the way I was getting the JsonObjec was by doing this in Routes.php:

我解决了我的问题,如果有人需要,我会分享它。所以我获得 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()));
} 
});

But instead I did this to actually access the value of user (Tom).

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

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

This solve my issue. Hope it helps anyone out there!

这解决了我的问题。希望它可以帮助那里的任何人!

回答by billi

Using Laravel the data can also be accessed using illuminate getData()method.

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

$someVar->getData();

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

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

回答by aleksejjj

With Laravel you can access to JSON data same way as regular variables. In your case you need something like:

使用 Laravel,您可以像访问常规变量一样访问 JSON 数据。在您的情况下,您需要类似的东西:

$username = $request->get('user');