Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

php - Posting JSON To Laravel

I am trying to make a post request of json to Laravel. The request is received on the server however when I try to access a property I get: "Trying to get property of non-object". On the client I'm using angularjs.

angular:

$http.post($rootScope.globals.basePath+"login/handleAjax",{"id" : obj.values[0].id,"profileUrl" : obj.values[0].publicProfileUrl}).success(function(data){
             console.log("got success!",data);
         });

laravel:

class LoginController extends BaseController {
/*User logs in to linkedin and sends his id through ajax to this function*/
public function handle_ajax() {
    $data = Input::all();
    *//Clockwork is just a debugging extension I'm using*
    Clockwork::info($data->id); **//"Trying to get property of non-object".**
}

Note: I can see in Fiddler that the JSON being sent is valid and that it reaches the controller+method (http 200).

The post request itself (As seen with Fiddler)

Headers: 
Accept: application/json, text/plain, */*
...
Text View:
{"id":"my id","profileUrl":"http://www.linkedin.com/pub/yoel-blum/51/373/76"}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update: Laravel 5

Please note as of Laravel 5.0, the Input facade has been removed from the official documentation (and in 5.2 it was also removed from the list of default Facades provided) in favor of directly using the Request class that Input invokes, which is IlluminateHttpRequest.

Also, as of the Laravel 5.1 documentation, all references to the Request facade have been removed, again in preference of using the IlluminateHttpRequest instance directly, which it encourages you to do via dependency injection in either:

...your Controller Method:

namespace AppHttpControllers;

use IlluminateHttpRequest;

class UserController extends Controller
{
    public function update(Request $request, $id)
    {
        $data = $request->json()->all();
    }
}

...or a Route Closure (as of 5.3):

use IlluminateHttpRequest;

Route::get('/', function (Request $request) {
    $data = $request->json()->all();
});

json() and ParameterBag

It's worth noting that $request->json() returns an instance of SymfonyComponentHttpFoundationParameterBag, and that ParameterBag's ->all() method returns an associative array, and not an object as the OP expected.

So one would now fetch the rough equivalent of $_POST['id'] as follows:

$data = $request->json()->all();
$id = $data['id'];

`Input` and `Request` facades: Current Status

Both facades have been removed from the official documentation (as of 5.1), and yet they both also remain in the source code with no 'deprecated' label.

As mentioned earlier, Input was removed as a default facade ('alias') in 5.2, but as of 5.4, the Request facade remains a default.

This seems to imply that one could still use the Request facade to invoke methods on the Request instance (e.g. Request::json()), but that using dependency injection is simply now the officially preferred method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...