我一直在尝试让它工作,概念上它只需单击标签,它就会调用一个返回JSON结果的REST服务,我会抓取国家名称并将其显示为测试。 我正在使用角度。
每次点击它都会返回状态为0。 相信这是pl http://plnkr.co/edit/k3Z6Ufi734oYE4ciVJs8
这是HTML 只需通过ng-click
调用GetInfo函数就可以了<!DOCTYPE html>
<html ng-app="mainModule">
<!--http://plnkr.co/edit/k3Z6Ufi734oYE4ciVJs8-->
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<link href="style.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<div><b ng-click="GetInfo()">Click Me</b></div>
<b>{{AdditionalInfo.geobytescountry}}</b>
</body>
</html>
这是Angular Back end。
var app = angular.module('mainModule', []);
app.controller('MainCtrl', function($scope, $http) {
$scope.name = 'World';
var cityDetailsUrl = "http://gd.geobytes.com/GetCityDetails?callback=?&fqcn=new%20york,%20NY,%20United%20States";
$scope.AdditionalInfo = {};
$scope.GetInfo = function ()
{
$http.get(cityDetailsUrl)
.success(function(data, status, header, config){
console.log('ok');
$scope.AdditionalInfo = data;
console.log(data);})
.error(function(data, status, header, config){
console.log('error');
$scope.AdditionalInfo = data;
console.log(status);});
}
});
提供的链接应该导致JSON响应,因为我在Web浏览器中尝试了id。 类似于“geobytesinternet”:“美国”,“geobytescountry”:“美国”
答案 0 :(得分:2)
在您的Plunker示例中,这似乎是两个问题。
首先,您似乎想要使用JSONP
,因此您应该使用$http.jsonp
而不是$http.get
,简单$http.get
会因同一原始政策违规而失败
另一个问题是,您的网址应该具有callback=JSON_CALLBACK
而不是callback=?
的属性才能使JSONP正常工作。修复这两个问题后,代码似乎正常工作。
查看Plunker的编辑版本:http://plnkr.co/edit/SYB9TI29MOHfNPw1jjQT
答案 1 :(得分:1)
找到它。由于服务器返回的是jsonp而不是json,因此修改您的网址以包含JSON_CALLBACK
:
http://gd.geobytes.com/GetCityDetails?callback=JSON_CALLBACK&fqcn=new%20york,%20NY,%20United%20States
并使用$http.jsonp
代替$http.get
。这是修改过的Plunker。