AngularJS jsonp不为我工作
我没有在html页面中获取数据。 home.htmlAngularJS jsonp不为我工作,angularjs,Angularjs,我没有在html页面中获取数据。 home.html <!Doctype html> <html ng-app="myServiceApp"> <head> <title>Processing $http.jsonp() response in service</title> </head> <body> <div ng-controller="myServiceCt
<!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>
您的HTML页面中没有任何内容。因此,无论是angular还是您的脚本都不会被加载和执行
添加脚本,一切都会好起来。直到包含angular.js文件,它就像普通的html页面一样。
使用下面的代码作为angular应用程序运行此页面。
<!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>
正在处理服务中的$http.jsonp()响应
var app=angular.module('myServiceApp',[]);
app.controller('myServiceCtrl',函数($scope,$http){
$scope.doJSONPRequest=函数(){
变量url=”http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=JSON_CALLBACK";
$http.jsonp(url)
.success(函数(数据、状态、标题、配置){
console.log(数据)
$scope.details=data.found;
$scope.statcode=状态;
}).error(函数(数据、状态、标题、配置){
$scope.statcode=状态;
});
}
});
单击并发出JSONP请求
数据详细信息:{{Details}}
状态代码:{{statcode}}
您的HTML页面中没有任何
。因此,angular和您的脚本都不会被加载和执行。除此之外,您的代码工作正常:
<!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>