2016-10-19 4 views
0

私のアプリですべての$ httpリクエストのデフォルトデータパラメータを送信します。 私はこの回答$httpInterceptorを参照しました。私はデフォルトのヘッダーを設定することができますが、パラメータは動作しません。私はJsonオブジェクトにプッシュする配列を送信したいと思い、これを試しました。私app.moduleで

angular 
    .module('app.main') 
    .factory('myHttpInterceptor', myHttpInterceptor); 

myHttpInterceptor.$inject = ['Constant']; 

function myHttpInterceptor(Constant) { 
    var data = [{'foo': 'fooBar'}]; 
    return { 
     request: requestInterceptor 
    }; 

    function requestInterceptor(config) { 
     config.params.push(data); 
     return config; 
    } 
} 

私はこの

app.config([...,'$httpProvider'...){ 
$httpProvider.interceptors.push('myHttpInterceptor'); 
} 

を指定した後にエラーは、私が

fooのようなデフォルトのパラメータを送信したいconfig.params.push

で来ます:fooBar

すべての私のhttp要求のために、ありがとうございます

+0

角度バージョン?この '$ httpProvider.interceptors'が1.1.4で追加されたためです。 – Sajan

+0

要求に 'params'が必ずしも存在するとは限りません。 – devqon

+0

angularjsのバージョンが1.4.4 –

答えて

0

私はプラム時に現在のパラメタ配列を持っていないと思います。このコードを試してください。

config.params arrayが存在しない場合は、初期化してから押します。

angular 
    .module('app.main') 
    .factory('myHttpInterceptor', myHttpInterceptor); 

myHttpInterceptor.$inject = ['Constant']; 

function myHttpInterceptor(Constant) { 
    var data = [{'foo': 'fooBar'}]; 
    return { 
     request: requestInterceptor 
    }; 

    function requestInterceptor(config) { 
     if(config.params.length != 0) 
     { 
      config.params.push(data); 
     } 
     else 
     { 
      config.params = [] 
      config.params.push(data); 
     } 
     return config; 
    } 
} 

は、これが存在する場合のparamsにプッシュ、または新規のparams

これはあなたの問題を解決する必要があり

を作成します。

+0

@Karthik Girraj、これを試しましたか? – Sravan

+0

遅れて申し訳ありません。 –

+0

config.params.pushは関数ではありません –

0

未定義の場合は、配列を作成する必要があります。

function myHttpInterceptor(Constant) { 
    var data = [{'foo': 'fooBar'}]; 
    return { 
     request: requestInterceptor 
    }; 

    function requestInterceptor(config) { 
     config.params = config.params || []; 
     config.params.push(data); 
     return config; 
    } 
} 
+0

同じエラー - config.params.pushは関数ではありません –

+0

@KarthikGirrajつまり、config.paramsは既に存在し、配列ではありません。 config.paramsを使用するかどうかわからない場合は、新しいconfigプロパティを設定してみてください。 – krutkowski86

関連する問題