2016-10-22 7 views
0

私はAjaxを使用してPOSTを実行しています。私のサーバーはデータを正常に取得しています。しかし、私はユーザーが送信した値にアクセスするのに苦労しています。簡単な言葉で "user"(tom)の値にどのようにアクセスできますか?誰でも私を正しい道に乗せてください。前もって感謝します。ここに私のJsonResponseオブジェクトがあります:PHPまたはLaravelの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] => 
) 

答えて

1

:あなたのケースでは、あなたのような何かを必要としています。 だから私は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())); 
} 
}); 

しかし、その代わりに、私は実際にユーザ(トム)の値にアクセスするためにこれをしました。

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

これは私の問題を解決します。誰も助けてくれることを願っています!

0

Laravelでは、通常の変数と同じ方法でJSONデータにアクセスできます。私は私の問題を解決し、私は、誰かがそれを必要とする場合にはそれを共有するつもりです

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

これは私が私のサーバーで取得JsonObjectには適用されません。私はユーザーの価値にアクセスする方法を見つける必要があります。 – HenryDev

+0

@HenryDev Hmm .. 'content()'メソッドはどうでしょうか? https://laravel.com/api/5.3/Illuminate/Http/JsonResponse.html – aleksejjj

関連する問題