問題描述
我通過ajax向我的控制器發(fā)送數(shù)據(jù)
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 現(xiàn)在應該可以工作了.
Hope this help. Your ajax should work now.
這篇關于無法在控制器 Laravel 5.1 中檢索通過 ajax 發(fā)送的數(shù)據(jù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!