2014-09-02 88 views
0

我正在使用角度爲我的應用程序和Web開發。我使用$ http進行數據發送和接收,但在執行真正的請求之前,我總是有這些OPTIONS請求。禁用或緩存AngularJS選項請求

我知道這些請求的目的,但這將是一個massiv負載沒有和我有我的服務器上提供的所有交叉源頭。

有沒有什麼辦法只緩存OPTIONS請求或禁用這些請求?我不想使用x-www-form-urlencoded。

任何想法?

+0

此緩存應該通過這將狀態的選項的有效期由指定服務器設置報頭響應中的年齡(以秒爲單位)。所以標題將包含:AccessControlMaxAge:3600 – Jon 2014-09-02 15:20:47

回答

0

您可以使用interceptor爲禁用請求

angular.module('myApp') 
.factory('myInterceptor', 
function ($q) { 
    var interceptor = { 
     'request': function (config) { 

     if(config.method === 'OPTIONS') return; 
     } 
    }; 
    return interceptor; 
}); 

並將其註冊到配置

angular.module('myApp') 
    .config(function ($httpProvider) { 
    $httpProvider.interceptors.push([ 
     '$injector', 
     function ($injector) { 
      return $injector.get('myInterceptor'); 
     } 
    ]); 
+0

TypeError:無法讀取$ http.serverRequest(http://192.168.178.20/lib/ionic/js/ionic.bundle.js:16965:25)中未定義的 的屬性「標題」 ) – mrmow 2014-09-03 16:46:24

+0

你確定這個錯誤取決於我建議的代碼嗎? – 2014-09-03 19:48:56

+0

很確定。我只是將你的行添加到我的代碼中。我不能發佈整個代碼,但我粘貼它的行。 http://pastebin.com/Asrd98A1 – mrmow 2014-09-04 20:45:45