unable to retrieve data sent via ajax in controller Laravel 5.1(无法在控制器 Laravel 5.1 中检索通过 ajax 发送的数据)
问题描述
我通过ajax向我的控制器发送数据
I am sending data via ajax to my controller as
$.ajax({
url: 'test',
type: 'POST',
data: { id: sessionStorage.getItem('user_id') },
dataType: 'json',
contentType: "application/json; charset=utf-8"/*,
success:function(id){
alert(sessionStorage.getItem('user_id'));
}*/
});
在我使用的控制器中
public function getUserMessages(){
$id = Input::get('id');
$messages = Message::where('message_by' , Auth::user()->id)->where('message_for',$id)->get();
echo "id is ",$id;
return $messages;
}
我在 $id
中什么也没得到.我也试过 $_POST['id']
表示 undefined index id
.我如何检索 id 值?$request->has('id')
也返回 false.
I am getting nothing in $id
. I have also tried $_POST['id']
which says undefined index id
. How I can retrive the id value?
$request->has('id')
returns false too.
推荐答案
问题是你设置的应用内容为json,不需要设置内容.
The problem is that you are setting the application content as json, You don't need to set the content.
jQuery ajax
contentType(默认:'application/x-www-form-urlencoded; charset=UTF-8')
contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')
$.ajax({
url: 'test',
type: 'POST',
data: { id: sessionStorage.getItem('user_id') },
dataType: 'json',
success:function(data){
console.log(data); // always good to output content for debugginn
}
});
希望对您有所帮助.你的 ajax 现在应该可以工作了.
Hope this help. Your ajax should work now.
这篇关于无法在控制器 Laravel 5.1 中检索通过 ajax 发送的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法在控制器 Laravel 5.1 中检索通过 ajax 发送的数


- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01