我将根据服务调用的响应在记录更新时添加一些警报。
当我检查响应中是否有任何数据时:
if (response.data === null)
这部分似乎没有得到遵守,并且显示了已成功更新记录的警报,并且如果您在屏幕快照中查看,则response.data为null。
我不确定简单的if是否无法正常工作,或者我是否正确地查看了响应对象。
有人可以帮忙,以便我了解如何执行此操作的最佳做法吗?
谢谢。
图片更新
最佳答案
希望这将有助于解释为什么您没有得到期望的结果(阅读代码注释):
function boooooom(msg) {
alert('We\'re sorry folks! ' + msg);
}
$http({
method: 'PUT',
url: 'api/StorePage/PutStorePage?StorePageId=' + this.newpage.StorePageID,
data: storePage,
})
.then(
// This callback will be called when the response is *available*
function(response) {
// Change this to `response.data` instead of `!response.data`
if (response.data) {
$scope.pages = response.data;
alert('Congrats!');
} else {
boooooom('No data');
}
},
// This callback is called if an error occurs or if the server
// returns a response with an *error* status.
//
// *Your* request hasn't caused an error as it *has* responded
// and the response is a 200 status (which is not an error status)
// so this callback doesn't get called.
function(response) {
boooooom('Houston we have a problem');
}
)
// It looks like you always want to `clearFields` for both failure and success
// so you can just do that here, which will do that for you.
.finally(clearFields);
关于javascript - AngularJS:当检查对象为null时,返回错误消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40185193/