如何从javascript将变量发布到Laravel 5路由?

如何从javascript将变量发布到Laravel 5路由?,javascript,laravel-5,routes,Javascript,Laravel 5,Routes,我试图在单击时从javascript发送一个简单的通知id,以标记通知已读 我花了几个小时在上面,但是我得到了一个无休止的500服务器错误 “此路由不支持GET方法。支持的方法:POST。” 谁能解释我做错了什么 我刀上的形状是这样的 <form method="POST" action="{{ route('markAsRead', $notification->id) }}"> @csrf <button type="submit" class="btn re

我试图在单击时从javascript发送一个简单的通知id,以标记通知已读

我花了几个小时在上面,但是我得到了一个无休止的500服务器错误 “此路由不支持GET方法。支持的方法:POST。”

谁能解释我做错了什么

我刀上的形状是这样的

<form method="POST" action="{{ route('markAsRead', $notification->id) }}">
  @csrf
  <button type="submit" class="btn read-notification unread" id="notificationIcon-{{ $notification->id }}">
  <i class="fas fa-envelope fa-2x"></i></br>
  <small>Mark as read</small>
  </button>
</form>
    $(".read-notification").on("click", function (e) {

        if ($(this).hasClass('unread')) {
            var mainDiv = $(this).closest(".notification");
            var divId = $(mainDiv).attr("id");
            var notificationId = divId.replace("notification-", "");

            document.getElementById("notificationIcon-"+notificationId).innerHTML = "<i class=\"far fa-envelope-open fa-2x\"></i>";
            $(this).removeClass('unread');
            $(this).addClass('read');
            $('.collapse').collapse('toggle');


            $.ajax({
                type: 'post',
                url: 'markAsRead',
                data: {'notificationId' : notificationId},
                dataType: 'json',
                success: function(response){
                    console.log("That worked!");
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
                }
            });


  //          $(".read-notification").attr("action", "/" + notificationId + "/markAsRead");
      }
    })
Route::post('markAsRead', 'NotificationController@markAsRead');
    public function markAsRead(Request $request) {

        $user = \Auth::user();
        $notification = $user->notifications()->where('id', $request->notificationId)->first();
        if ($notification) {
            $notification->markAsRead();
            return back();
        } else {
            return back()->withErrors('Sorry, we had a problem');
        }

    }
我的控制器是这样的

<form method="POST" action="{{ route('markAsRead', $notification->id) }}">
  @csrf
  <button type="submit" class="btn read-notification unread" id="notificationIcon-{{ $notification->id }}">
  <i class="fas fa-envelope fa-2x"></i></br>
  <small>Mark as read</small>
  </button>
</form>
    $(".read-notification").on("click", function (e) {

        if ($(this).hasClass('unread')) {
            var mainDiv = $(this).closest(".notification");
            var divId = $(mainDiv).attr("id");
            var notificationId = divId.replace("notification-", "");

            document.getElementById("notificationIcon-"+notificationId).innerHTML = "<i class=\"far fa-envelope-open fa-2x\"></i>";
            $(this).removeClass('unread');
            $(this).addClass('read');
            $('.collapse').collapse('toggle');


            $.ajax({
                type: 'post',
                url: 'markAsRead',
                data: {'notificationId' : notificationId},
                dataType: 'json',
                success: function(response){
                    console.log("That worked!");
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
                }
            });


  //          $(".read-notification").attr("action", "/" + notificationId + "/markAsRead");
      }
    })
Route::post('markAsRead', 'NotificationController@markAsRead');
    public function markAsRead(Request $request) {

        $user = \Auth::user();
        $notification = $user->notifications()->where('id', $request->notificationId)->first();
        if ($notification) {
            $notification->markAsRead();
            return back();
        } else {
            return back()->withErrors('Sorry, we had a problem');
        }

    }
但目前我无法通过这条路线

我尝试过命名路由和URL,但总是出现相同的错误


非常感谢您的帮助。

在1.9.0中添加了使用
方法:“post”
,而不是
类型:“post”
方法。从这个版本开始,
type
method
的别名。如果您使用的是1.9.0之前版本的jQuery,那么应该使用
type
Switched to method,但结果仍然相同。我在任何地方都没有看到csrf标记。也许这就是原因?CSRF令牌在刀片中的形式,刚刚添加到问题中。使用
方法:“post”
,而不是
类型:“post”
方法是在1.9.0中添加的。从这个版本开始,
type
method
的别名。如果您使用的是1.9.0之前版本的jQuery,那么应该使用
type
Switched to method,但结果仍然相同。我在任何地方都没有看到csrf标记。也许这就是原因?CSRF令牌在刀片中的形式,刚刚添加到问题中。