【问题标题】:Laravel ajax POST method not allowedLaravel ajax POST 方法不允许
【发布时间】:2018-12-15 00:03:00
【问题描述】:

我尝试在管理面板中存储有关客户端的 cmets。
在 Laravel 5.5 中发送 ajax 请求时出现错误 405(不允许方法)
当我使用没有 ajax 的 html 表单发送数据时,一切正常。

阿贾克斯:

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

$(".save-comment").click(function(e) {
    e.preventDefault();
    var id = $("#hidden-id").val();
    var type = $("#slect-comment-type option:selected").val();
    var text = $("#comment").val();
    console.log(id);
    console.log(type);
    console.log(text);
$.post("storeComment", { id: id, type: type, text: text })
    .done(function(data) {
    console.log(data);
    alert("Success");
    $("#add-comment").css("display", "none");
    })
    .fail(function() {
    alert("Fail");
    });
});

正如我在控制台中看到的那样,我在 { id: id, type: type, text: text } 中传递了正确的数据。

路线:

Route::post('/comments/store' , 'CommentController@store')->name('storeComment');

评论控制器:

public function store(Request $request) {
    $id = $request->input('id');
    $type = $request->input('type');
    $text = $request->input('text');

    $comment = new Comment();
    $comment->client_id = $id;
    $comment->type = $type;
    $comment->text = $text;
    $comment->created_by = $request->user()->first_name.' '.$request->user()->last_name;
    $comment->save();
return response()->json(['success'=>'Success']);
}

HTML:

<form class="form-horizontal" method="POST" action="{{route ('storeComment')}}">
    {{ csrf_field() }}
    <input id="hidden-id" type="hidden" name="id" value="{{$id}}">
    <div class="form-group">
        <label class="col-md-4 control-label" for="commentSelect">Comment type</label>
    <div class="col-md-6">
        <select id="slect-comment-type" name="type" class="form-control">
            <option value="Call">Call</option>
            <option value="Mail">Mail</option>
        </select>
    </div>
    </div>
    <div class="form-group{{ $errors->has('comment') ? ' has-error' : '' }}">
        <label for="comment" class="col-md-4 control-label">Comment</label>
    <div class="col-md-6">
        <textarea name="text" class="form-control" rows="5" id="comment"></textarea>
        @if ($errors->has('comment'))
        <span class="help-block">
        <strong>{{ $errors->first('comment') }}</strong>
        </span>
        @endif
    </div>
    </div>
    <div class="form-group">
    <div class="col-md-6 col-md-offset-4">
        <button type="submit" class="btn btn-primary btn-sm save-comment">Save</button>
    </div>
    </div>
</form>

【问题讨论】:

  • 错误。 ajax 中应该有 $.post("storeComment",而不是 $.post("cmets.store")。

标签: ajax laravel-5


【解决方案1】:

用“/”解决了

$.post("/storeComment", { id: id, type: type, text: text })
    .done(function(data) {
    console.log(data);
    alert("Success");
    $("#add-comment").css("display", "none");
    })

【讨论】:

    【解决方案2】:

    将 store_comment_form id 赋给 form 然后:

    $('#store_comment_form').on('submit',(function(e){
    
                    e.preventDefault();
                    var data = new FormData(jQuery('#store_comment_form')[0]);
                    $.ajax({
                        url:"{{url('/comments/store')}}",
                        type:'POST',
                        data: data,
                        success:function(response){
    
                        }
                    });
                }));
    

    【讨论】:

    • 仍然不允许使用 405 方法。
    • 分享截图
    • 添加了屏幕截图。我尝试使用 url 请求:/cmets/store、cmets/store、storeComment。还是同样的问题。
    【解决方案3】:
    $.post("comments/store", { id: id, type: type, text: text })
        .done(function(data) {
        console.log(data);
        alert("Success");
        $("#add-comment").css("display", "none");
        })
        .fail(function() {
        alert("Fail");
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2017-06-08
      • 2018-07-16
      • 1970-01-01
      • 2018-03-07
      • 2017-05-31
      • 1970-01-01
      • 2018-06-15
      • 2019-05-10
      • 2017-12-09
      相关资源
      最近更新 更多