2016-11-08 3 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> 
관련 문제