簡體   English   中英

使用AngularJS $ http處理狀態304響應

[英]Handle Status 304 response with AngularJS $http

如果我有一個API服務器,則該API將以JSON格式發送ajax數據:

{"status":304,"message":"Cannot delete data where PK is empty or > 1"}

如何在AngularJS $ http帖子中調用狀態和消息來提醒Bootbox? 這是我的AngularJS $ http帖子

$http({
  method: "POST",
  url: apiUrl('disable_assethw'),
  data: {
    id: id
  },
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
}).then(function successCallback(response) {
  if(response.status == 304) {
    bootbox.alert("Something went error.." + response.data.message);
  } else {
    $scope.getAssetHW();
  }
}, function errorCallback(response) {
  bootbox.alert("Something went error.." + response.status);
});

感謝您的指教。

您說這是json響應,並且使用了: application / x-www-form-urlencoded ,這是錯誤的。

處理rest / api調用的最佳實踐是:

創建1個可以在整個應用程序中訪問的通用/通用函數,該函數將管理您的post api調用(將api響應添加到回調中):

postAPICall(url, body, data) {
    let headers = new Headers({'Content-Type': 'application/json'});
    this.http
        .post(url,
            body, {
                headers: headers
            })
        .map(
            response => response.json())
        .subscribe(
            response => {
                data(response);
            },
            err => data(this.handleError(err)); //handle error here
        );
}

在需要時(在組件或服務中)調用此函數:

var yourJSONBody = {
    "param-1": "",
    "param-2": "",
    //....
    }
}

this.myCommonService.postAPICall("localhost:8080/app/", yourJSONBody, data => {
    if (data.status == "304") {
        //do stuff
        //this.msgs.push({severity: 'error', detail: data.message});
    }
    else {
        //do stuff
    }
});

錯誤處理程序功能:

private handleError(error: any) {
    let description = 'There was an error: ' + error.status;
    let errors = {
        errorcode: error.status,
        errorstatus: error.statusText,
        errordescription: description
    };
    return errors;
}

當以JavaScript對象作為數據執行POST請求時,請使用AngularJS默認內容類型(該內容類型自動設置為application/json )。 $ http服務還會自動將JavaScript對象編碼為JSON字符串

成功處理程序僅處理狀態在200-299范圍內的響應。 超出范圍的狀態由拒絕處理程序處理:

$http({
  method: "POST",
  url: apiUrl('disable_assethw'),
  data: {
    id: id
  },
  headers: {
    ̶'̶C̶o̶n̶t̶e̶n̶t̶-̶T̶y̶p̶e̶'̶:̶ ̶'̶a̶p̶p̶l̶i̶c̶a̶t̶i̶o̶n̶/̶x̶-̶w̶w̶w̶-̶f̶o̶r̶m̶-̶u̶r̶l̶e̶n̶c̶o̶d̶e̶d̶'̶
  }
}).then(function successCallback(response) {
  ̶i̶f̶(̶r̶e̶s̶p̶o̶n̶s̶e̶.̶s̶t̶a̶t̶u̶s̶ ̶=̶=̶ ̶3̶0̶4̶)̶ ̶{̶
    ̶b̶o̶o̶t̶b̶o̶x̶.̶a̶l̶e̶r̶t̶(̶"̶S̶o̶m̶e̶t̶h̶i̶n̶g̶ ̶w̶e̶n̶t̶ ̶e̶r̶r̶o̶r̶.̶.̶"̶ ̶+̶ ̶r̶e̶s̶p̶o̶n̶s̶e̶.̶d̶a̶t̶a̶.̶m̶e̶s̶s̶a̶g̶e̶)̶;̶
   ̶}̶ ̶e̶l̶s̶e̶ ̶{̶
    $scope.getAssetHW();
  ̶}̶
}, function errorCallback(response) {
  //HANDLE 304 status HERE
  if(response.status == 304) {
    bootbox.alert("Something went error.." + response.data.message);
  } else {
    bootbox.alert("Something went error.." + response.status);
  };
});

從文檔中:

在200到299之間的響應狀態代碼被視為成功狀態,並將導致調用成功回調。 任何超出該范圍的響應狀態代碼均被視為錯誤狀態,並會導致錯誤回調被調用。 同樣,將小於-1的狀態代碼歸一化為零。 -1通常表示請求已中止。

— AngularJS $ http服務API參考

注意:狀態-1通常表示瀏覽器拒絕了具有違反同源策略CORS問題的請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM