1

Here's the snippet of code where I get a JSON response :

app.controller('apiController', function($scope, $http) {
$http.get("http://api.openweathermap.org/data/2.5/forecast/daily?q=Kansas+City&mode=json&units=metric&cnt=10&appid=f6b7081abd94e66273817ed6b5ce95c7").then(function(response) {
    $scope.myData = response.data.list;
});
});

What are some of the best practices to handle say an empty/garbage/null JSON response?

4 Answers 4

2

By using AngularJS $httpProvider.interceptors we can handle request and response globally.Here I am adding code for response only. First create one angular factory e.g.

myModule.factory('myResponseInterceptors', function ($q) {
    return {
        response: function (response) {
            // do something on success
        if(response.data == null)
       {
        // Do what ever you want to do 
       }
            return response;
        },
        responseError: function (response) {
            // do something on error
            return $q.reject(response);
        }
    };
});

And add this factory into $httpProvider on your main module config section

myModule.config(function ($httpProvider) {
    $httpProvider.interceptors.push('myResponseInterceptors');
});

AngularJs httpPromise response object has following properties
data – {string|Object} – The response body transformed with the transform functions.
status – {number} – HTTP status code of the response.
headers – {function([headerName])} – Header getter function.
config – {Object} – The configuration object that was used to generate the request.
statusText – {string} – HTTP status text of the response.

Sign up to request clarification or add additional context in comments.

Comments

1

Very simple, like this:

app.controller('apiController', function($scope, $http) {
$http.get("http://api.openweathermap.org/data/2.5/forecast/daily?q=Kansas+City&mode=json&units=metric&cnt=10&appid=f6b7081abd94e66273817ed6b5ce95c7").then(function(response) {
    if(response.data){
        $scope.myData = response.data.list;
    }else{
        // TODO: your data is null or undefined
    }
});
});

Comments

1

add this check

if(response.data) {
  if(response.data.list) {
    $scope.myData=response.data.list;
  }
}

Comments

1

I don't know if this is the best answer but you can handle it in the following way,

angular.isUndefined(val) || val === null

app.controller('apiController', function($scope, $http) {
   $http.get("http://api.openweathermap.org/data/2.5/forecast/dailyq=Kansas+City&mode=json&units=metric&cnt=10&appid=f6b7081abd94e66273817ed6b5ce95c7").then(function(response) {
   $scope.myData = response.data.list;
   if(angular.isUndefined($scope.myData) || $scope.myData === null)
   {
     //Type an user friendly error msg for the end user and show on view
      $scope.errorMsg = "Sorry...the list is empty".
     // handle hide and show of this error msg whenever $scope.mydata is undefined or null.
   }
});
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.