【发布时间】:2014-02-08 18:22:46
【问题描述】:
我正在尝试向 Laravel 发出 json 的发布请求。该请求是在服务器上收到的,但是当我尝试访问我得到的属性时: “试图获取非对象的属性”。 在客户端我使用 angularjs。
角度:
$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".**
}
注意:我可以在 Fiddler 中看到正在发送的 JSON 是有效的,并且它到达了控制器+方法 (http 200)。
发布请求本身(如 Fiddler 所示)
Headers:
Accept: application/json, text/plain, */*
...
Text View:
{"id":"my id","profileUrl":"http://www.linkedin.com/pub/yoel-blum/51/373/76"}
【问题讨论】:
-
不是
Input::json()->all()吗? -
Laravel 不知道你已经发布了 JSON。您必须自己
json_decode或使用尚未记录的Input::json()->all(),尽管在源 (github.com/laravel/framework/blob/master/src/Illuminate/Http/…) 中。 -
似乎没有解决它:$data = Input::json()->all();返回 $data->id;我得到同样的错误......