检查omdnapi中的空响应

Check null response in omdnapi

本文关键字:响应 omdnapi 检查      更新时间:2023-09-26

我想使用omdbapi获取电影信息…我能够得到的信息,当它是一个有效的电影标题,我想显示一个消息,当标题不存在。代码有什么问题?

$scope.searchMovie= function(){
      $http.get('http://www.omdbapi.com/?t='+$scope.name+'&y='+$scope.year+'').success(function (response) {
        var len=response.length;
        if(len === null)
        {
            alert("No records found!");
        }
        else{
        $scope.movieSearch=response;
        }
      });
  };

Api response有response字段。您可以使用此字段来确定是否找到了电影(respone.Response=="True")或未找到(respone.Response=="False")。

试试这个:

$scope.searchMovie= function(){
      $http.get('http://www.omdbapi.com/?t='+$scope.name+'&y='+$scope.year+'').success(function (response) {
        console.log(response);
        if (response.Response == "False")
        {
            alert("No records found!");
        }
        else if (response.Response == "True")
        }
      });
  };