【问题标题】:POST localhost/.. 405 (Method Not Allowed)POST localhost/.. 405(不允许的方法)
【发布时间】:2018-08-26 16:20:38
【问题描述】:

我正在使用 Ajax 向控制器发送请求,但每次我收到此错误:

刷新页面时出现这两个错误:

1) Uncaught ReferenceError: $ is not defined

2) Uncaught ReferenceError: Holder is not defined

当我发送请求时出现这个错误:

jquery-3.3.1.min.js:2 POST localhost/.. 405 (Method Not Allowed)
send @ jquery-3.3.1.min.js:2
ajax @ jquery-3.3.1.min.js:2
(anonymous) @ 0:221
dispatch @ jquery-3.3.1.min.js:2
y.handle @ jquery-3.3.1.min.js:2

我的代码:

$.ajax({
    type:'POST',
    data:
    {
        "_token":$("input[name=_token]").val(),
        'question_id':question_id,
        'user_id':user_id
    },
         href:"{{route('favorites.reds')}}",
         success: function (data) {
             console.log(data);
    }
})

路线:

Route::post('/questions/reds', "FavoritesController@addToReds")->name('favorites.reds');

输入:

 <i class="fa fa-heart" style="margin-right: 100px; cursor: pointer; " aria-hidden="true">
                        <input type="hidden" value="{{$question->id}}" class="question_fav">
                        <input type="hidden" value="{{Auth::user()->id}}" class="user_fav">
                    </i>

谁能解决这个问题?

【问题讨论】:

    标签: php jquery ajax laravel


    【解决方案1】:

    看起来您可能在 $.ajax 函数中使用了不正确的语法。 将href:"{{route('favorites.reds')}}" 切换到url:"{{route('favorites.reds')}} 看看会有帮助。

    【讨论】:

    • 我这样做了,现在我得到了这个:POST localhost/.. 419 (unknown status)
    • 您能否确认“_token”字段正确发布 - 不正确的“_token”字段有时会导致 419 错误
    • 我没有在任何地方发布 csrf。我该怎么做?
    • 试试这个:$.ajax({ type:'POST', data: { "_token": '{{csrf_token()}}', 'question_id':question_id, 'user_id':user_id }, url:"{{route('favorites.reds')}}", success: function (data) { console.log(data); } })
    • 但问题与控制器无关,一切都在控制器之前
    猜你喜欢
    • 2014-05-23
    • 2016-09-27
    • 2018-12-20
    • 2015-11-16
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    相关资源
    最近更新 更多