【问题标题】:laravel ajax, How to send form having value='put' successfully?laravel ajax,如何成功发送具有 value='put' 的表单?
【发布时间】:2016-11-23 08:19:16
【问题描述】:

我正在制作功能,如果使用 ajax 发送编辑表单,则验证并将响应发送到视图。但是,页面加载到新的 URL 上,例如 localhost:8000/comment/id。该页面显示我想要的消息。在视图未重新加载的情况下如何获得该响应?

HTML

<form class="modify-form" method="post" action="{{ route('comment.update', $mypage->id) }}">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <input type="hidden" name="_method" value="put">
    <p class="comment-message">{{ $mypage->message }}</p>
</form>

路线

Route::match(['put', 'patch'], 'comment/{id}', ['as'=>'comment.update', 'uses'=>'MyPageController@update']);

jQuery

var $writeForm = $('form.visitor');
$writeForm.find('.write').click(function(e){

    var url = $writeForm.attr('action');

    e.preventDefault();
    $.ajax({
        url: url,
        type: 'POST',
        data: $writeForm.serialize(),
        success: function(res){
            if (res.status) {
                location.reload();
            }
        },
        error: function(res){
                $writeForm.after('<div class="alert alert-danger" role="alert">'+res.responseJSON.errors+'</div>')
                //console.log(res.responseJSON.errors);
        }

    });

});

控制器

public function update($id) {

    $validator = validator::make($data = Input::all(), mypage::$edit);

    if($validator->passes()) {

        $mypage = mypage::findOrFail($id);

        $message = Request::input('message');
        $mypage->update(['message' => $message]);

        $mypage->save();

        $response = array();
        $response['status'] = true;

        return response()->json($response);

    }

    if ($validator->fails()) {

        $response = array();

        $response['status'] = false;
        $response['errors'] = $validator->errors()->all();

        return response()->json($response, 400);

    }

}

【问题讨论】:

  • 我会推荐使用 Vue.js,我知道这可能不是你想要的答案,但这是一个很好的建议,因为 vue.js 轻量级易于使用并且与 Laravel 具有很好的兼容性。它允许您轻松地从后端提取和发布数据,并且与普通的 Ajax 方法相比,它有很多方法可供您使用。

标签: javascript php jquery ajax laravel


【解决方案1】:

您可以为评论创建一个部分(小视图)。

然后,当您创建/更新评论时,Laravel 可以返回评论的视图,并使用 JQuery 将其附加到 HTML。

您可以为评论创建部分:(非常简单)

&lt;p&gt;{{$comment}}&lt;/p&gt;

您可以像任何视图一样返回部分:

return view('partials.comment')-&gt;with('comment', $comment);

然后在 JS 中追加如下:

$("#comments-container").append(res);

【讨论】:

  • 我可以要求一些例子吗?
【解决方案2】:

试试这个

阿贾克斯:

type: $('input[name="_method"]').val(),
data: $writeForm.serialize(),
success: function(res){
    if (res.status == true) {
        // Reload the current page, without using the cache
        document.location.reload(true);
    }
},

控制器:

if ($validator->fails()) {
    return response()->json([
        'status' => false,
        'errors' => $validator->errors()->all()
    ], 400);

}

if($validator->passes()) {
    $mypage = mypage::findOrFail($id);

    $message = Request::input('message');
    $mypage->update(['message' => $message]);
    $mypage->save();

    return response()->json(['status' => true], 200);
}

【讨论】:

  • 刚刚在新的 url 上,显示 {"status":false,"errors":["The message field is required."]}
  • {"status":false,"errors":["The message field is required."]} 是对的,但我想把它放在我的页面上。不是新页面
  • 现在您需要在查看页面中显示此消息。在错误中使用 jquery each 函数:function() {}` 块
  • 错误:函数(res){ $writeForm.after('' ) //console.log(res.responseJSON.errors); } 错了吗?
  • 我以为没有回复。
猜你喜欢
  • 2019-05-09
  • 2011-06-13
  • 2016-11-25
  • 2020-04-02
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
相关资源
最近更新 更多