Laravel 5:从$request中检索JSON数组。

时间:2022-09-13 13:20:42

I'm a Laravel newbie and I'm converting a php/jquery app to Laravel. The original code used a JSON array with an ajax POST, which was retrieved like this:

我是一个Laravel新手,我正在把一个php/jquery应用程序转换成Laravel。原始代码使用一个JSON数组和一个ajax POST,它是这样检索的:

$json = file_get_contents('php://input');
$data = json_decode($json,true);

I'm doing much the same thing on the POST side, but I don't see any data coming through in my Laravel $request collection. Is there something special that I need to do to retrieve JSON data structured like this:

我在POST方面做了很多相同的事情,但是我没有看到在我的Laravel $request集合中有任何数据。我需要做一些特殊的事情来检索JSON数据结构:

[
    { "name": "John", "location": "Boston" }, 
    { "name": "Dave", "location": "Lancaster" }
]

Here is my jQuery ajax POST code (with hard coded data)

这是我的jQuery ajax POST代码(带有硬编码数据)

$.ajax({
    type: "POST",
    url: "/people",
    data: '[{ "name": "John", "location": "Boston" }, { "name": "Dave", "location": "Lancaster" }]',
    dataType: "json",
    success:function(data) {
        $('#save_message').html(data.message);
    } 
});

Here is the code in my Controller that receives the POST

这是我的控制器中接收邮件的代码。

public function store(Request $request)
{
    dd($request->all());
}

But all I get is:

但我得到的只有:

[]

[]

Any ideas on how I can retreive my data?

有什么办法可以让我的数据恢复吗?

2 个解决方案

#1


14  

You need to change your Ajax call to

您需要更改您的Ajax调用。

$.ajax({
    type: "POST",
    url: "/people",
    data: '[{ "name": "John", "location": "Boston" }, { "name": "Dave", "location": "Lancaster" }]',
    contentType: "json",
    processData: false,
    success:function(data) {
        $('#save_message').html(data.message);
    } 
});

change the dataType to contentType and add the processData option.

将数据类型更改为contentType并添加processData选项。

To retrieve the JSON payload from your controller, use:

要从控制器检索JSON有效负载,请使用:

dd(json_decode($request->getContent(), true));

instead of

而不是

dd($request->all());

#2


12  

 $postbody='';
 // Check for presence of a body in the request
 if (count($request->json()->all())) {
     $postbody = $request->json()->all();
 }

This is how it's done in laravel 5.2 now.

这就是现在的laravel 5.2。

#1


14  

You need to change your Ajax call to

您需要更改您的Ajax调用。

$.ajax({
    type: "POST",
    url: "/people",
    data: '[{ "name": "John", "location": "Boston" }, { "name": "Dave", "location": "Lancaster" }]',
    contentType: "json",
    processData: false,
    success:function(data) {
        $('#save_message').html(data.message);
    } 
});

change the dataType to contentType and add the processData option.

将数据类型更改为contentType并添加processData选项。

To retrieve the JSON payload from your controller, use:

要从控制器检索JSON有效负载,请使用:

dd(json_decode($request->getContent(), true));

instead of

而不是

dd($request->all());

#2


12  

 $postbody='';
 // Check for presence of a body in the request
 if (count($request->json()->all())) {
     $postbody = $request->json()->all();
 }

This is how it's done in laravel 5.2 now.

这就是现在的laravel 5.2。