Php 从JSON post中,Laravel总是为空
我正在开发Laravel 5.2版。我一直在尝试从post获取json数据。我总是得到空数据。我也尝试过使用互联网上的解决方案,但没有任何效果。比如说,Php 从JSON post中,Laravel总是为空,php,json,laravel,laravel-5,Php,Json,Laravel,Laravel 5,我正在开发Laravel 5.2版。我一直在尝试从post获取json数据。我总是得到空数据。我也尝试过使用互联网上的解决方案,但没有任何效果。比如说, 1. $request->json()->all(); 2. Input::get('data'); 3. $request->get('data'); 4. $request->data; 5. Input::all(); 6. json_decode(request()->getContent(),
1. $request->json()->all();
2. Input::get('data');
3. $request->get('data');
4. $request->data;
5. Input::all();
6. json_decode(request()->getContent(), true);
7. json_decode($request->getContent(), true);
8. json_decode(request()->get('payload'));
9. json_decode($request->get('payload'));
10. $request->input('data');
这是我的javascript代码
$.ajax({
method: 'POST',
url: url,
contentType: 'application/json',
headers: {
'X-CSRF-TOKEN': token
},
data: {'data':'foo'},
success: function(data) {
console.log(data);
}
});
这是我的拉威尔密码
public function postJsonData(Request $request){
//several methods I am trying to use.
$data = $request->data;
Log::info($data);
return json_encode($data);
}
现在我真的没有想法继续前进。我真的需要帮助。谢谢您的时间。我认为您是通过POST方法获得JSON负载的,因此在这种情况下,您可以这样做
$json = file_get_contents('php://input');
$data = json_decode($json);
您需要流式传输数据,让我知道它是否适合您我认为此代码有助于您:
public function postJsonData(Request $request)
{
$data = $request->input('data');
Log::info($data);
return response()->json(data)
}
你试过使用
数据:{data:'foo'},
?我认为除了URL之外,一切都是正确的。尝试在此处发布您的路线。另外,在JS变量url
中,尝试写入var url=“{{url('your/url')}}”
。