【问题标题】:Laravel Ajax Call through entire pageLaravel Ajax 调用整个页面
【发布时间】:2017-10-06 08:34:00
【问题描述】:

我正在尝试通过我的整个应用程序运行 Ajax 发布调用,它将更新导航。在某些页面上它可以工作,但在其他页面上却不行,我该如何解决这个问题并使其成为全球性的。

我使用 Laravel 作为 php 框架。

# Middleware group if user is logged in
Route::group(['middleware' => 'auth'], function () {
     # Notifications
    Route::group(['prefix' => 'notification', 'as' => 'notification.'], function () {
        Route::post('number', ['as' => 'number', 'uses' => 'NotificationController@number']);
    });
    Route::group(['prefix' => 'relation', 'as' => 'relation.'], function () {
        Route::get('show/{id}', ['as' => 'show', 'uses' => 'RelationController@show']);
    });
});

在我的 layouts/app.blade.php 我包含这样的 js 文件

<script src="{{ asset('js/liveUpdater.js') }}"></script>
@yield('javascript')

liveUpdater ajax 函数

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
    }
});
$.ajax({
    url: 'number',
    type: 'post',
    success: function(data) {
        $('#number-of-notifications').text(data.unread);
    },
    error: function(data) {
        console.log('error number ' + data.data);
    }
});

Url http://localhost/myApp/public/notification/all 返回一条成功消息。

但是像这样http://localhost/myApp/public/relation/show/1这样的url会返回错误消息:

number
/myApp/public/relation/show
405
Method Not Allowed

【问题讨论】:

  • 当您向定义为接受 GET 请求的路由发出 POST 请求或向定义为接受 POST 的路由发出 GET 请求时,您会收到此错误

标签: javascript php jquery ajax laravel


【解决方案1】:

你在路由前加上notification,所以你的ajax请求应该指向notification/number

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
    }
});
$.ajax({
    url: 'notification/number',
    type: 'post',
    success: function(data) {
        $('#number-of-notifications').text(data.unread);
    },
    error: function(data) {
        console.log('error number ' + data.data);
    }
});

另外我认为别名(在组中)不会有帮助,所以我认为(为简单起见)你可以:

Route::group(['middleware' => 'auth'], function () {
     # Notifications
    Route::group(['prefix' => 'notification'], function () {
        Route::post('number', 'NotificationController@number']);
    });
});

Routing groups docs

【讨论】:

  • 这在这样的 uri 中不起作用:http://localhost/app/public/relation/show/1 或这个 http://localhost/app/public/notification/all 它返回 404,如果我打开 http://localhost/app/public/notification/all 它指向 /app/public/notification/notification/number 例如
  • 啊...它们没有定义,因为我可以在您提供的路线中清楚地看到...
  • 它们只是 Ajax URL 以错误的方向发布它们,我在上面的问题中添加了关系/显示路由作为示例
【解决方案2】:

您必须为每个 url 路径定义路由路径和相应的控制器方法,例如关系/显示和通知/全部:

Route::group(['middleware' => 'auth'], function () {
     # Notifications
    Route::group(['prefix' => 'notification'], function () {
        Route::post('show/{show}', 'NotificationController@show']);
        Route::post('number', 'NotificationController@number']);
        Route::post('all ', 'NotificationController@all']);
    });
});

【讨论】:

    【解决方案3】:

    您在 ajax 中的请求方法有误。它应该是类型:“GET”,或者在你的 web.php 中像 Route::post('show/{id}' 而不是 Route::get('show/{id}'

    您的请求方法与抛出 405 的原因不匹配

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      • 2021-06-02
      • 2014-02-21
      • 2011-02-04
      • 2019-03-30
      相关资源
      最近更新 更多