dousi2029 2014-12-07 18:27
浏览 70
已采纳

使用AJAX提交表单laravel

I am trying to add comment using AJAX technology but I have an error: Failed to load resource: http://localhost:8888/blog/public/comment/add the server responded with a status of 500 (Internal Server Error) Here is my code: View:

{{ Form::open(array('method'=>'post','class'=> 'col-md-6','url' => '/comment/add', 'id'=>'comment')) }}
                        <input type="hidden" name="post_id" value="{{$id}}">
                        <div class="row">
                            <div class="inner col-xs-12 col-sm-12 col-md-11 form-group">
                                {{Form::label('name', 'Imię')}}
                                {{Form::text('username', null, array('class'=>'form-control', 'id'=>'name', 'name'=>'name'))}}
                            </div>
                            <div class="inner col-xs-12 col-sm-12 col-md-12 form-group">
                                {{Form::label('message', 'Wiadomość')}}
                                {{Form::textarea('message', null, array('class'=>'form-control', 'id'=>'message', 'name'=>'message', 'rows'=>'5'))}}
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xs-12 col-md-12 submit form-group">
                                {{Form::submit('Wyślij', array('name'=>'submit', 'class'=>'btn btn-orange'))}}
                            </div>
                        </div>

                    {{ Form::close() }}

Controller:

public function addComment()
{
        $this->layout = null;
        //check if its our form
        if(Request::ajax()){
            $name = Input::get( 'name' );
            $content = Input::get( 'message' );

            $comment = new Comment();
            $comment->author =  $name;
            $comment->comment_content = $content;
            $comment->save();

            $postComment = new CommentPost();
            $postComment->post_id = Input::get('post_id');
            $postComment->comment_id = Comment::max('id');
            $postComment->save();

            $response = array(
                'status' => 'success',
                'msg' => 'Setting created successfully',
            );
            return 'yea';
        }else{
            return 'no';
        }
}

AJAX:

    jQuery( document ).ready( function( $ ) {

    $( '#comment' ).on( 'submit', function(e) {
        e.preventDefault();

        var name = $(this).find('input[name=name]').val();

        $.ajax({
            type: "POST",
            url: host+'/comment/add',
        }).done(function( msg ) {
            alert( msg );
        });

    });
});

And the last one routes:

Route::post('comment/add', 'CommentController@addComment');

Anyone have an idea where is the problem and why I can't submit my form?

  • 写回答

2条回答 默认 最新

  • douzi9430 2014-12-07 18:45
    关注

    You are not posting any data,

        $.ajax({
            type: "POST",
            url: host+'/comment/add',
        }).done(function( msg ) {
            alert( msg );
        });
    

    The error you are getting is that the columns in DB cannot be null.

    Try to change your ajax call to this:

        $.ajax({
            type: "POST",
            url: host+'/comment/add',
            data: { name:name, message:message, post_id:postid }, 
            success: function( msg ) {
                alert( msg );
            }
        });
    

    Change this

    var name = $(this).find('input[name=name]').val();
    

    to

    var name = $('#name').val();
    

    and fetch the message and the post id:

    var message = $('#message').val();
    var postid = $('#post_id').val();
    

    Complete ajax block:

       $('#comment').on('submit', function(e) {
           e.preventDefault(); 
           var name = $('#name').val();
           var message = $('#message').val();
           var postid = $('#post_id').val();
           $.ajax({
               type: "POST",
               url: host+'/comment/add',
               data: {name:name, message:message, post_id:postid}
               success: function( msg ) {
                   alert( msg );
               }
           });
       });
    

    And finally, add an ID to the hidden field:

    <input type="hidden" name="post_id" id="post_id" value="{{$id}}">
    

    Send data back from Laravel controller, eg.

        // ........
    
            $response = array(
                'status' => 'success',
                'msg' => 'Setting created successfully',
            );
            return Response::json($response);  // <<<<<<<<< see this line
        }else{
            return 'no';
        }
    }
    

    This will send the data in your response back to your ajax request.

    Then, alter your ajax success function:

     // .......
     success: function( msg ) {
         $("body").append("<div>"+msg+"</div>");
     }
    
     // ..........
    

    You will now see that a new div was created in your <body> including the created response. If you want to show the newly created post, just create it as the ajax response and append it to any element in your page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形