如何在PHP或Laravel中的JsonResponse对象内访问属性值? [英] How to access a property value inside JsonResponse Object in PHP or Laravel?

查看:131
本文介绍了如何在PHP或Laravel中的JsonResponse对象内访问属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ajax进行POST,并且我的Server正在很好地获取数据.但是,我正在努力访问用户发送的值.简单来说,我该如何访问"user"(tom)的值?任何人都可以使我走上正确的道路.先感谢您.这是我的JsonResponse对象:

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:

[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] => 
)

推荐答案

我已解决了我的问题,如果有人需要,我将与他人分享. 所以我得到JsonObjec的方法是在Routes.php中做到这一点:

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:

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()));
} 
});

但是我实际上是通过这样做来访问用户(Tom)的值的.

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

这解决了我的问题.希望它能帮助任何人!

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

这篇关于如何在PHP或Laravel中的JsonResponse对象内访问属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆