日志消息,如果 Api 服务状态 == 404 在角度 JS

log message if Api service status == 404 in angularJS

本文关键字:JS 状态 消息 如果 Api 服务 日志      更新时间:2023-09-26

我的服务中有此代码.js

function myService($http, $log) {
 var API = 'https://myapi.net/api/api.php?';
 $log.debug(API);
 return {
  getApi: function(inputValue, inputValue2) {
    return $http({
      url:API + inputValue + '=' + inputValue2,
      method:'GET'
    })
  }
 };
}

在我的控制器中:

vm.searchNetflix = function() {
  netflixService.getApi(vm.searchResult, vm.searchResult2).then(function(result){
    vm.ResultSearch = result.data;
    $log.log('result' + result);
  });
};

这 2 个值"搜索结果和搜索结果"我在输入中调用,并更改 api 网址,

例: https://myapi.net/api/api.php?mySearchResult=mySearchResult2

当我输入一个值时,它会返回正确的内容,如果我键入的内容无效,则返回 404 或 400,如果 a 值为空。

如何才能有效此状态,并在我视图中返回?

谢谢:)

将信息传递回视图在很大程度上取决于视图的构造方式,但原则是确保正确捕获错误条件。

这类似于文档中关于$http的示例;您需要提供另一种方法来覆盖错误条件。

vm.searchNetflix = function() {
  netflixService.getApi(vm.searchResult, vm.searchResult2)
    .then(function(result) {
      vm.ResultSearch = result.data;
      $log.log('result' + result);
    }, function(errorResult) {
      // extract information from the errorResult here
    });
};

理想情况下,确定状态错误原因所需的任何信息都应在 errorResult 对象中提供。