SyntaxError: Unexpected token u at Object.parse (native) in Ionic

时间:2016-02-03 04:04:35

标签: javascript json ajax ionic-framework

I keep on encountering this error:enter image description here

RecipeService.js

recipeDetails: function(recipe_id) {
      $http({
        method: 'POST',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        url: 'http://localhost/admin-recipick/home/ajaxSelectRecipes',
        data: recipe_id
      }).then(
        function success(response) {
          returnRecipeData = response.data;
          console.log(response.data);

        },
        function error(response) {
          returnRecipeData = response.data;
          console.log(response.data);
          // handle error
        }

      );
  }

controllers.js

$scope.recipeDetails = function(recipe_id) {
      var getDataRecipe = getRecipeService.recipeDetails(recipe_id);
      if (getDataRecipe != "null") {
          RecipeService.setObject('recipick', getDataRecipe);
          var s = RecipeService.getObject('recipick');
          console.log(s)
          $state.go('app.porkmenudo');

      } else {
          $ionicPopup.alert({
              title: 'Login failed!',
              template: 'Please check your Credentials!'
          });
      }
}

As you can see in the RecipeService its returning the right object I want but if i go to controller and get the object its giving me error. And also I have this factory getting the object

.factory('RecipeService', function($window) {
    return {
        set: function(key, value) {
            $window.localStorage[key] = value;
        },
        get: function(key, defaultValue) {
            return $window.localStorage[key] || defaultValue;
        },
        setObject: function(key, value) {
            $window.localStorage[key] = JSON.stringify(value);
        },
        getObject: function(key) {
            return JSON.parse($window.localStorage[key] || '{}');
        },
        clearStorage: function(key) {
            $window.localStorage.removeItem(key);
        }
    }
});

What should I do?

1 个答案:

答案 0 :(得分:0)

Your recipeDetails needs to return a promise. Currently the service method is not actually returning any data.