dongliang1654 2013-11-22 19:18
浏览 19
已采纳

Ajax for laravel 4

I'm trying to set up ajax for laravel, but it won't work. I have controller for posting comments. javascript which blocks the submit button and sends ajax which is returning 500 error

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>

What it returns is 500 internal server error

UPD:

    Route::get('posts/{id}', ['as' => 'post', 'uses' => 'PostsController@getShow'])->where('id', '\d+'); // Where id == number
Route::get('category/{id}', ['as' => 'category', 'uses' => 'PostsController@getCategory'])->where('id', '\d+');

SOLUTION: I used in constructor csrf protection. after i removed csrf protection from postComment it worked for me Route::controller('posts', 'PostsController');

  • 写回答

1条回答 默认 最新

  • dongxiangchan0743 2013-11-22 20:26
    关注

    It is difficult to to offer advice without seeing your route, controller, view code neatly separated. However, this is the bare minimum necessary to achieve your desired outcome:

    In your routes.php

    Route::post('/comment', function()
    {
        return Response::json(['blah' => 'ohhh']);
    });
    

    In your view:

    $.ajax({
        type : "POST",
        url : "comment",
    });
    

    if you are getting a 500 response then it is likely that you have a problem with your route to controller@postComment.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法