2017-01-30 5 views
1

私は一度に二回、このディレクティブを呼んでいるdataServiceパラレル取得要求 - 角度JS

dataService.getMachineTaxesById($scope.machineId).then(function (response) { 
    $scope.machine.machineTaxes = response.data; 
}); 

を持ってmachineFormという名前のディレクティブを抱えています。 getMachineTaxesByIdため

<div class="modal-compare-item"> 
    <machine-form app-data="appData" 
     index="analyzie_index" 
     machine-id="machineAnalyze.id" 
     machine="machineAnalyze" 
     action="'edit'"> 
    </machine-form> 
</div> 
<div class="modal-compare-item"> 
    <machine-form app-data="appData" 
     index="compare_index" 
     machine-id="machineCompare.id" 
     machine="machineCompare" 
     action="'edit'"> 
    </machine-form> 
</div> 

dataServiceはここにある:

// Get Machine Taxes By Id 
this.getMachineTaxesById = function(machine_id) {   
    return $http({ 
     method: 'GET', 
     url: 'https:xyz/api/MachineTaxes/Machine/'+ machine_id, 
     headers: { 
      'Accept': 'application/json', 
      'Content-Type': 'application/json', 
      'Authorization' : $cookies.get('token_type') + " " + $cookies.get('access_token') 
     } 
    }); 
}; 

の両方を一度に呼び出されたとき、私は<machine-form>のいずれかのコメントはありません場合、それは正常に動作します。私はresponseと言っています{"message":"Authorization has been denied for this request."}

一度に並列要求を送信するのは何ですか? 1つのリクエストが完了するのを待ってから、他のリクエストを送信する必要があります。

注:リクエストごとにアクセストークンを使用しています。

+0

ブラウザの*ネットワークのルック*コンソール。失敗したリクエストを探し、 'Authorization'リクエストヘッダーを確認してください。それは適切に設定されていますか?これらのクッキー値はいつ設定されますか( 'token_type'と' access_token')? – Phil

+1

Angularsパースペクティブから複数の同時リクエストを行うことは問題ありません。エンドポイントは特定のトークンの同時接続を制限しているのでしょうか? – Brian

+0

@Brian他にも成功したリクエストがあります。同じAPI urlを複数回試してみると、それは '{" message ":"この要求に対して承認が拒否されました。 "と応答します。} –

答えて

3

、サービスを順次XHRsを実行する必要があり、それから、前の呼び出しチェーンからの約束を保存するには:

app.service("dataService", function($http) { 
    var lastPromise; 
    // Get Machine Taxes By Id 
    this.getMachineTaxesById = function(machine_id) { 
     var config = { 
       method: 'GET', 
       url: 'https:xyz/api/MachineTaxes/Machine/'+ machine_id, 
       headers: { 
        'Accept': 'application/json', 
        'Content-Type': 'application/json', 
        'Authorization': 
          $cookies.get('token_type') + " " + $cookies.get('access_token') 
       }; 
     if (!lastPromise) {   
      lastPromise = $http(config); 
     } else { 
      lastPromise = lastPromise.catch(function (errorResponse) { 
       //convert rejected promise to success 
       return errorResponse; 
      }).then(function(response) { 
       //return httpPromise to chain 
       return $http(config); 
      }); 
     }; 
     return lastPromise;  
    }; 
});