【问题标题】:Posting JSON To Laravel将 JSON 发布到 Laravel
【发布时间】: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;我得到同样的错误......

标签: php angularjs laravel


【解决方案1】:

更新:Laravel 5

请注意,从 Laravel 5.0 开始,Input 外观已从 official documentation 中删除(在 5.2 中,它也是提供的默认外观的 removed from the list),以支持直接使用Input 调用的Request 类,即Illuminate\Http\Request

此外,从 Laravel 5.1 documentation 开始,所有对 Request 的引用 facade 都已被删除,再次优先使用 Illuminate\Http\Request 实例直接,它通过依赖注入 encourages you to do

...你的控制器方法:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

...或 Route Closure(从 5.3 开始):

use Illuminate\Http\Request;

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

json() 和 ParameterBag

值得注意的是$request->json() 返回Symfony\Component\HttpFoundation\ParameterBag 的实例,而ParameterBag->all() 方法返回关联数组,而不是OP 预期的对象。

所以现在可以获取 $_POST['id'] 的大致等价物,如下所示:

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

`Input` 和 `Request` 外观:当前状态

这两个外观都已从官方文档中删除(截至5.1),但它们在source code 中的both also remain 没有“已弃用”标签。

如前所述,Input was removed 在 5.2 中作为默认外观(“别名”),但在 5.4 中,Request 外观 remains a default

这似乎暗示一个可以仍然使用Request门面来调用请求实例上的方法(例如Request::json()),但是使用依赖注入只是现在官方首选的方法。

【讨论】:

  • 知道如何从 ParameterBag 而非数组中检索 stdClass 对象吗?
  • @DrakeES 您只需将其转换为对象:(object) Request::json()->all() 因为ParameterBag 的参数在内部存储为关联数组,目前没有“作为对象返回“ 选项。另请参阅此问题:stackoverflow.com/questions/19272011/…
  • @Greendrake 请参阅this thread 了解有关此问题的有趣历史讨论,包括来自 T.O. 的评论。他自己(看起来很眼熟,哈哈)。
  • @jdunk 我可以在帖子中发送一些参数,在 json 中发送一些参数吗?
【解决方案2】:

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

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

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

【讨论】:

  • 我没有使用all()而是json(),会输出接收到的json
【解决方案3】:

为了扩展(和纠正)上述内容,在 Laravel 5 中,您将检索 JSON,如下所示:

public function handle_ajax(Request $request) {
    $data = (object) $request->json()->all();
    Clockwork::info($data->id);
}

在重要的示例中,您可能还想先验证您的输入。

【讨论】:

  • 感谢 [间接] 让我注意到这一点 @Pete。我当时的回答对于 Laravel 5.0 是正确的,但确实是后来(当 5.1+ 出现时)然后说“在 Laravel 5 中,你想要的是 Request::json->all()”有点不准确 - - 即使这实际上仍然完全有效 - 因为它已从文档(尽管不是源代码)中删除,以支持您在此处指出的内容。我相应地更新了我的答案。
猜你喜欢
  • 2012-10-05
  • 1970-01-01
  • 2012-05-29
  • 2017-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多