2017-08-28 93 views
0

響應,如果我有一個API服務器,則API與JSON格式發送AJAX數據:

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

如何AngularJS $ HTTP POST調用狀態和消息,以提醒bootbox? 這裏我的AngularJS $ http post

$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); 
}); 

感謝您的建議。

+0

你說過它的json,在這裏你提到'{'Content-Type':'application/x-www-form-urlencoded'}' – harishr

+0

除非你改變了默認'requestTransformer','{id:id} ''application/x-www-form-urlencoded'請求肯定不是有效負載 – Phil

+0

等待,請問您的問題頂部的JSON是來自服務器的響應負載嗎?如果是這樣,你想'if(response.data.status == 304)' – Phil

回答

0

使用JavaScript對象作爲數據執行POST請求時,請使用AngularJS的默認內容類型(自動設置爲application/json)。 $http service也自動將JavaScript對象編碼爲JSON strings

只有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 Service API Reference

注: -1狀態通常表明瀏覽器拒絕了CORS problem違反same-origin policy請求。

0

你說這是JSON響應和你使用的:應用程序/ x-WWW窗體-urlencoded,這是不對的。

處理其餘/ API調用最好的做法是:

創建1個普通/一般功能是在整個應用程序的訪問將管理您的文章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; 
} 
+0

如果您遇到任何問題或錯誤,或者我缺少一些東西,請發帖給我。 –

+0

你的答案使用Angular 2+'http' API。問題是AngularJS [$ http Service](https://docs.angularjs.org/api/ng/service/$http) – georgeawg