Php 用于Laravel4的Ajax
我正在尝试为laravel设置ajax,但它不起作用。 我有管理员发布评论。javascript阻止提交按钮并发送返回500错误的ajaxPhp 用于Laravel4的Ajax,php,jquery,ajax,laravel,laravel-4,Php,Jquery,Ajax,Laravel,Laravel 4,我正在尝试为laravel设置ajax,但它不起作用。 我有管理员发布评论。javascript阻止提交按钮并发送返回500错误的ajax public function postComment() { if (Request::ajax()) { return Response::json(['blah' => 'ohhh']); } } $('#submit_comment').click(function (e) {
public function postComment()
{
if (Request::ajax())
{
return Response::json(['blah' => 'ohhh']);
}
}
$('#submit_comment').click(function (e)
{
e.preventDefault();
$.ajax({
type : "POST",
url : "http://page.dev/posts/comment",
data :
{
comment : $("#comment_area").text()
}
});
});
<script type="text/javascript">var comment = "{{URL::action('PostsController@postComment')}}"; </script>
解决方案:
我在csrf保护中使用过。在我将csrf保护从Post Comment中移除后,它对我起了作用
路由::控制器('posts','PostsController') 如果没有看到路线、管制员、视图代码整齐地分开,就很难提供建议。但是,这是实现预期结果所必需的最低要求: 在routes.php中
Route::post('/comment', function()
{
return Response::json(['blah' => 'ohhh']);
});
在你看来:
$.ajax({
type : "POST",
url : "comment",
});
如果您收到的回复为500,那么很可能您的访问路线有问题controller@postComment.您的500服务器响应可能会出现错误消息(希望如此)。你能查一下吗?应该是回应的一部分。最后,你能告诉我们代码在哪些文件中吗?在上面的代码段中,您似乎将控制器代码与视图代码混合在一起。很抱歉,响应太晚。。。使用您在Route中展示的简单示例,它是有效的。。。但是,当我尝试将它附加到控制器时,它会显示500。我只知道它与Route::post('posts/comment',function(){});,但是如果我加上['uses'=>”它就不起作用了PostsController@postComment']
$.ajax({
type : "POST",
url : "comment",
});