Did I miss anything with this $http.jsonp in angularjs?

时间:2015-06-26 09:46:51

标签: angularjs

I know this question has been asked several times, but I'm not understanding if I missed anything or it's just a server thing. I need to show you my code for checking please:

(function(){

var app = angular.module('c4s', ['ionic', 'starter.controllers', 'starter.services']);

app.controller('curiousCtrl', function($http, $scope){
    $scope.stories = [];

    $http.jsonp('http://curious4science.com/?json=1&callback=JSON_CALLBACK')

        .success(function(response) {
            angular.forEach(response.data.children, function(child){
                console.log(response);
                $scope.stories.push(child.data);
            });
        });
});

app.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {

    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleLightContent();
    }
  });
})

.config(function($httpProvider, $stateProvider, $urlRouterProvider) {
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
        $stateProvider

      // setup an abstract state for the tabs directive
        .state('tab', {
        url: "/tab",
        abstract: true,
        templateUrl: "templates/tabs.html"
  })

  // Each tab has its own nav history stack:

  .state('tab.dash', {
    url: '/dash',
    views: {
      'tab-dash': {
        templateUrl: 'templates/tab-dash.html',
        controller: 'DashCtrl'
      }
    }
  })

  .state('tab.chats', {
      url: '/chats',
      views: {
        'tab-chats': {
          templateUrl: 'templates/tab-chats.html',
          controller: 'ChatsCtrl'
        }
      }
    })
    .state('tab.chat-detail', {
      url: '/chats/:chatId',
      views: {
        'tab-chats': {
          templateUrl: 'templates/chat-detail.html',
          controller: 'ChatDetailCtrl'
        }
      }
    })

  .state('tab.account', {
    url: '/account',
    views: {
      'tab-account': {
        templateUrl: 'templates/tab-account.html',
        controller: 'AccountCtrl'
      }
    }
  });

  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/tab/dash');

});
}());

And I'm getting this error:

TypeError: response.data is undefined

First, I tried $http.get(....) but I got the error that cross domain thing is not allowed. Then I tried jsop.

Thank you.

1 个答案:

答案 0 :(得分:1)

The sucess() callback doesn't take the http response as argument. It takes the response data, then the status, then the headers, then the config.

So the code should be

$http.jsonp('http://curious4science.com/?json=1&callback=JSON_CALLBACK')
    .success(function(data) {
        angular.forEach(data.children, function(child){
            console.log(response);
            $scope.stories.push(child.data);
        });
    });

(assuming the returned JSON has a children field which is an array, and that each child has a data field, of course).