【问题标题】:Angular $http.get request not running success or error functionsAngular $http.get 请求未运行成功或错误函数
【发布时间】:2016-12-30 17:12:02
【问题描述】:

我正在尝试向 Nutritionix v1_1 api 发出获取请求。调试时,我可以看到函数被成功调用并传入了正确的数据。当函数遇到$http.get 请求时,它会跳过其余代码(.success.error 部分),然后不返回承诺。我知道这个请求很好,因为我已经使用邮递员成功提出了请求。我有这样的请求:

(此方法在工厂内部。稍后从控制器调用。)

let getNutrients = (foodId) => {
    let authPart =`appId=${NUTRITIONIXAPIKEY.appId}&appKey=${NUTRITIONIXAPIKEY.appKey}`;
    let filter = 'fields=item_name,item_id,brand_name,brand_id,item_type,nf_calories,nf_total_carbohydrate,nf_dietary_fiber,nf_cholesterol,nf_total_fat,nf_sugars,nf_sodium,nf_protein,images_front_full_url,nf_serving_size_qty,nf_serving_size_unit,nf_servings_per_container';
    // let sort = 'sort: {"field":"_score", "order":"desc"}';
    // let typefilter = '"filters":{"not": {"item_type":3}}';
    return (
        $q((resolve,reject) =>{             
            $http.get(`https://api.nutritionix.com/v1_1/item?id=${foodId}&${filter}&${authPart}`)
            .success( (response) => {
                console.log('nutrix response nutrients request', response);
                resolve(response);
            }).error(function(errorResponse){
                console.log('nutrix fail nutrients request', errorResponse);
                reject(errorResponse);
            });
        })
    );
};

这是来自控制器的工厂方法调用:

NutrixFactory.getNutrients(foodId).then(function(nutrients){
    console.log('nutrients returned', nutrients);
    // $scope.nutrients = $scope.nutrients || [];
    $scope.nutrients.push(nutrients);
    console.log('nutrients array', $scope.nutrients);
});

【问题讨论】:

  • 为什么又用 $q 了?因为 $http 也使用 $q
  • @ShankarShastri 我对此没有明确的答案。这就是我被教导提出请求的方式。我认为这可能与MVC有关。我从控制器调用这个函数,然后在函数调用上链接一个 .then 来处理返回的数据。
  • Angular 的哪个版本?
  • @tks2n Angular 只会在请求完成后运行您的回调。您可能正在寻找错误的地方来解决您的问题...

标签: javascript angularjs model-view-controller angular-promise angular-http


【解决方案1】:

然后尝试捕获成功和错误,如果它是 1.6.*

Deprecation Notice

let getNutrients = (foodId) => {
    let authPart =`appId=${NUTRITIONIXAPIKEY.appId}&appKey=${NUTRITIONIXAPIKEY.appKey}`;
    let filter = 'fields=item_name,item_id,brand_name,brand_id,item_type,nf_calories,nf_total_carbohydrate,nf_dietary_fiber,nf_cholesterol,nf_total_fat,nf_sugars,nf_sodium,nf_protein,images_front_full_url,nf_serving_size_qty,nf_serving_size_unit,nf_servings_per_container';
    // let sort = 'sort: {"field":"_score", "order":"desc"}';
    // let typefilter = '"filters":{"not": {"item_type":3}}';
    return (
        $q((resolve,reject) =>{             
           return $http.get(`https://api.nutritionix.com/v1_1/item?id=${foodId}&${filter}&${authPart}`)
            .then( (response) => {
                console.log('nutrix response nutrients request', response);
                resolve(response);
            }).catch(function(errorResponse){
                console.log('nutrix fail nutrients request', errorResponse);
                reject(errorResponse);
            });
        })
    );
};

【讨论】:

  • 即使是 1.6.x 之前的版本,仍应使用 .then().catch()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
  • 2019-01-01
  • 1970-01-01
  • 2015-05-25
  • 2017-05-02
相关资源
最近更新 更多