2017-10-06 5 views
1

私はアプリケーション全体を通してAjaxポストコールを実行しようとしていますが、ナビゲーションを更新します。いくつかのページでは動作しますが、他のページではそうではありませんが、どうすればこの問題を解決してグローバルにすることができますか。Laravel Ajaxページ全体を呼び出す

私はLaravelをPHPフレームワークとして使用しています。私のレイアウト/ app.blade.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]']); 
    }); 
}); 

私は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のURLのようなファイルがあります。

しかし、このhttp://localhost/myApp/public/relation/show/1ような例のURLは、エラーメッセージを返します:

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

GET要求を受け入れるように定義された経路にPOST要求を行うか、POSTを受け入れるように定義された経路にGET要求を送るときにこのエラーが発生する –

答えて

1

あなたはとてもあなたのAJAXリクエストがnotification/numberを指している必要がありますnotificationのルートを前に付けているの:

$.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); 
    } 
}); 

また、エイリアシング(グループ内)は役に立たないと思うので、(簡単にするために)あなたは持っていると思う:

+0

これはuriではうまくいきません: 'http:// localhost/app/public/notification/all'を開くと、404を返します。' http:// localhost/app/public/app/public/notification/all'それは '/ app/public/notification/notification/number'を指します – Jaan

+0

Ahh ...あなたが提供したルートではっきりとわかるように定義されていません... –

+0

はしかし、AjaxのURLがそれらを間違った方向に投稿した場合、上記の質問の中のリレーション/ルートを例として追加します – Jaan

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

あなた関係/ショーと通知/すべてのためのようなすべてのURLパスのルートパスと対応するコントローラメソッドを定義しました:リクエストメソッドで

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

間違いではアヤックスあなたのweb.phpに「GET」と入力するか、Route :: post( 'show/{id}'の代わりにroute.get( 'show/{id}')を入力してください。

それがなぜ投げかける405

関連する問題