【问题标题】:Laravel 5 AJAX submit internal server error 500Laravel 5 AJAX 提交内部服务器错误 500
【发布时间】:2015-09-18 19:14:43
【问题描述】:

为了解决这个问题,我已经在网上搜索了好几个小时。我对 AJAX 很陌生。因此,当我尝试将数据提交到控制器时,我在控制台中遇到了内部服务器 500 错误。我认为这是 CSRF 不匹配,但我已经尝试了所有我能想到的可以解决这个问题的方法。我在 layout.blade.php 中使用元标记方法进行 AJAX CSRF 提交。

<meta name="csrf-token" content="{{ csrf_token() }}">

<script>
    $(function() {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
    });
</script>

我的评论代码如下所示。我将 $p-&gt;id 传递给路由,该路由将传递给控制器​​。

@if(Auth::check())
    {!! Form::model($p, ['route' => ['new_comment.post', $p->id]]) !!}
        <div class = "comment submit">
            <textarea id = "comment_content" placeholder = "Your comment goes here!" name = "comment_content" class = "content_display comment"></textarea>
        </div>

        <div class = "action">
            <li><a class = "comment_button new" href = "#"><i class = "fa fa-check"></i></a></li>
        </div>
    {!! Form::close() !!}
@else
    <p><a href = "/login">Log in to post a comment</a></p>
@endif

我的 AJAX:

<script>
$('.comment_button.new').click(function(e) {
    e.preventDefault();

    var comment_content = $('#comment_content').val();
    var data = 'comment_content'+comment_content;
    var url =  window.location.pathname;

    $.ajax({
        type : "POST",
        url : url,
        data : data,
        success:function(data){
            alert(data);
        },
        error:function(){ 

        }
    });
    return false;
});

</script>

我的路线:

Route::post('/home/view/{id}', ['as' => 'new_comment.post', 'before' => 'csrf', 'uses' => 'ViewPost@new_comment']);

我的控制器:

class ViewPost extends Controller
{

public function show($id)
{
    $posts = Posts::where('id', '=', $id)->get();
    $comments = Comments::where('post_id', '=', $id)->get();
    return view('view')->with('posts', $posts)->with('comments', $comments);
}

public function new_comment(Request $request, $id)
{
    $comment_postID = $id;
    $comment_data = $request->input('comment_content');

    if($request->ajax()) {
        $com = new Comment;
        $com->post_id = $comment_postID;
        $com->comment_content = $comment_data;
        $com->save();
    }
}


}

编辑

这是问题http://pastebin.com/tNcN5bLP的堆栈跟踪

已解决

正在使用“评论”调用我的“评论”模型。我通过在末尾添加一个 s 来修复它。哎呀!

还有,

我使用 AJAX 不正确地发布数据,因此我的控制器无法读取输入。这个片段来自我的 AJAX

var data = 'comment_content'+comment_content;
data : data,

应该是

data : {comment_content:comment_content},

谢谢!

【问题讨论】:

  • 多考虑var url = window.location.pathname;,写类似这样的url并测试url: window.location.pathname; + '/home/view/' + commentId
  • @Pedrammarandi '/home/view/{{ $p->id }}' 提供与 window.location.pathname 相同的 url

标签: jquery ajax laravel internal-server-error


【解决方案1】:

在您的主模板(您总是为每个新页面扩展的模板)中尝试以下操作:

    <script>
        $(document).ready(function() {

                        $.ajaxSetup({
                            headers: {
                                'X-CSRF-TOKEN': "{{ csrf_token() }}"
                            }
                        });
        });
    </script>

确保它是刀片模板,以便正确处理大括号。

另外,请尝试启用 dubug 模式并检查响应以确保它不是完全别的东西,如果您使用问题的完整堆栈跟踪更新您的答案会更容易提供帮助。

【讨论】:

  • 我尝试了您的代码,但没有解决任何问题。这是我的堆栈跟踪。 pastebin.com/tNcN5bLP
  • 现在我错过了实际的异常(顶部的错误)
  • 很抱歉。看起来这是一个 BadMethodCallException。这缩小了很多。 pastebin.com/Dk5G6CqW
  • 想通了。 “评论”模型被调用为“评论”。错过了一个s。哎呀。现在要弄清楚“comment_content”为空的完整性约束违规。 pastebin.com/zn9Hhi34
  • 好吧,好消息是它与 csrf 令牌无关。从您问题的代码来看,评论似乎不理解 save() 这很奇怪。您确定要扩展 Model for Comment 吗?
猜你喜欢
  • 2015-09-20
  • 2015-11-22
  • 2019-12-05
  • 2016-05-14
  • 2016-10-02
  • 2017-04-12
  • 2020-02-12
  • 2018-04-16
  • 2016-03-24
相关资源
最近更新 更多