我想通過我的整個應用程序運行一個Ajax調用,它應該更新導航。在一些頁面上它可以工作,但在另一些頁面上它卻沒有,我該如何解決這個問題並使其成爲全球性的。Laravel 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' => '[email protected]']);
});
Route::group(['prefix' => 'relation', 'as' => 'relation.'], function() {
Route::get('show/{id}', ['as' => 'show', 'uses' => '[email protected]']);
});
});
在我的佈局/ 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);
}
});
網址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請求發送POST請求時,您會收到此錯誤 –