我在Laravel 5中的Ajax调用上遇到以下错误:
TypeError:' stepUp'调用了一个没有实现接口HTMLInputElement的对象。
d = [],E =函数(A,B){B = n.isFunction(b)中的b():空== B"":?B,
表格如下:
<form id="comment_form">
<input type="textarea" class="form-control_comment" id="comment" placeholder="Type your comment here...">
<input type="hidden" name="uid" id="uid" value="{{$user->id}}">
<input type="hidden" name="pid" id="pid" value="{{$post->id}}">
<input type="hidden" id="token" value="{{$csrf_token()}}">
<button type="button" class="btn btn-sm btn-success" id= "comment_button">Submit</button>
</form>
Ajax部分:
<script>
$("#comment_button").click(function(e){
e.preventDefault();
var token = $("#token").val();
var a= $("#comment").val();
var uid= $("#uid").val();
var url = "<?php echo Request::root();?>";
$.ajax({
url: url+"post/comment_action",
type: "POST",
async: false,
data: {newComment:a, uid:uid, pid:pid, _token: token},
success: function (newResult){
if(newResult!="")
{
newResult= JSON.parse(newResult);
$("#all_responses").append(newResult);
}
}
}).error(function()
{
alert("Error");
});
});
</script>
在routes.php中:
Route::post('post/comment_action', array('as'=>'post/comment_action', 'uses'=>'PostController@postComment_action'));
PostController中的动作部分:
public function postComment_action()
{
if(Auth::check())
{
if(Request::ajax())
{
$inputs= Input::all();
}
$new_comment= $inputs['newComment'];
$uid= $inputs['uid'];
$pid= $inputs['pid'];
$com= new Comment;
$com->content= $new_comment;
$com->userId= $uid;
$com->postId= $pid;
$com->save();
$data= array();
$all_comments= Comment::where('postId', $pid)->first();
$u= User::where('id', $uid);
foreach($all_comments as $coms)
{
$data= $coms->content;
}
$data= json_encode($data);
echo $data;
}
else
{
return Redirect::route('signin_user')->withErrors("You must sign in to perform this action");
}
}
非常感谢任何帮助:)