将 JSON 发布到 Laravel [英] Posting JSON To Laravel

查看:25
本文介绍了将 JSON 发布到 Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向 Laravel 发出 json 的 post 请求.请求是在服务器上收到的,但是当我尝试访问一个属性时,我得到:试图获取非对象的属性".在客户端上,我使用的是 angularjs.

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.

角度:

$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).

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

发布请求本身(如 Fiddler 所见)

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"}

推荐答案

注意:此答案仅适用于旧的 Laravel 版本(4.2 及更早版本)!

Laravel 的 Input::all 方法返回一个关联数组,而不是 PHP 的 stdClass 对象.

Laravel's Input::all method returns an associative array, not an object of PHP's stdClass.

$data = Input::all();
$data['id']; // The ID of the request

这篇关于将 JSON 发布到 Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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