2016-11-08 109 views
1

我沒有在html頁面中獲取數據。 home.html的AngularJS jsonp不適用於我

<!Doctype html> 

<html ng-app="myServiceApp"> 

<head>  
    <title>Processing $http.jsonp() response in service</title>   
</head> 

<body> 

<div ng-controller="myServiceCtrl"> 

    <h3>Processing $http.jsonp() response in service</h3> 

    <button ng-click="doJSONPRequest()">Click and make JSONP request</button>  
    <p>Data Details: {{details}}</p>  
    <p>Status Code: {{statcode}}</p>  
</div>  
</body> 
</html> 

myServiceCtrl.js

var app = angular.module('myServiceApp', []); 

app.controller('myServiceCtrl', function ($scope, $http) { 

    $scope.doJSONPRequest = function() { 

     var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=JSON_CALLBACK"; 

     $http.jsonp(url) 

      .success(function (data, status, headers, config) { 

       $scope.details = data.found; 

       $scope.statcode = status; 

      }) 

      .error(function (data, status, headers, config) { 

       $scope.statcode = status; 

      }); 

    } 

}); 
+0

您的HTML頁面中沒有任何「

1

直到你包含angular.js文件,就像普通的html頁面一樣。

使用下面的代碼來運行這個頁面作爲角度的應用程序。

<!DOCTYPE html> 
<html> 
<head>  
    <title>Processing $http.jsonp() response in service</title>   
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
    <script> 
    var app = angular.module('myServiceApp', []); 
     app.controller('myServiceCtrl', function ($scope, $http) { 

      $scope.doJSONPRequest = function() { 

       var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=JSON_CALLBACK"; 

       $http.jsonp(url) 

        .success(function (data, status, headers, config) { 

         console.log(data) 
         $scope.details = data.found; 
         $scope.statcode = status; 

        }).error(function (data, status, headers, config) { 

         $scope.statcode = status; 

        }); 

      } 

     }); 
    </script> 

</head> 

<body> 
    <div ng-app="myServiceApp" ng-controller="myServiceCtrl"> 
     <button ng-click="doJSONPRequest()">Click and make JSONP request</button>  
     <p>Data Details: {{details}}</p>  
     <p>Status Code: {{statcode}}</p>  
    </div>  
</body></html>