使用angular.js解析JSON文件
我想用angular.js解析json文件中的比特币价格 json源代码是 我用于angular.js的代码:使用angular.js解析JSON文件,json,angularjs,http,Json,Angularjs,Http,我想用angular.js解析json文件中的比特币价格 json源代码是 我用于angular.js的代码: <div ng-app="myApp" ng-controller="customersCtrl"> <ul> <li ng-repeat="x in price"> {{ x.15m + ', ' + x.last }} </li> </ul> </div> <scr
<div ng-app="myApp" ng-controller="customersCtrl">
<ul>
<li ng-repeat="x in price">
{{ x.15m + ', ' + x.last }}
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("https://blockchain.info/de/ticker")
.success(function (response) {$scope.price = response.USD;});
});
</script>
-
{x.15m+','+x.last}
var-app=angular.module('myApp',[]);
app.controller('customersCtrl',函数($scope,$http){
$http.get(“https://blockchain.info/de/ticker")
.success(函数(响应){$scope.price=response.USD;});
});
结果应该是15米的价格,然后用逗号分隔最新价格
我的猜测是,我在response.USD附近搞错了什么,但1小时后,我仍然没有找到正确的方法
请帮忙 看来您实际上从区块链站点得到了一个错误。他们在服务器上设置了单一来源策略,以保护他们免受点击劫持攻击。以下是您得到的错误:
XMLHttpRequest cannot load https://blockchain.info/de/ticker. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<your domain>' is therefore not allowed access.
XMLHttpRequest无法加载https://blockchain.info/de/ticker. 请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”。
您的选择是联系站点,让他们将您的服务器作为例外添加,或者使用来自服务器的某种curl请求,然后让您的GET请求命中您的本地副本
创建一个名为results.php的文件,输入以下代码:
<?php
try {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://blockchain.info/de/ticker'
));
$result = curl_exec($curl);
echo $result;
} catch (Exception $e) {
header("HTTP/1.0 500 Unable to pull ticker results");
echo $e->getMessage();
}
?>
现在在脚本中,只需将其更改为:
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("results.php")
.success(function (response) {$scope.price = response.USD;});
});
</script>
var-app=angular.module('myApp',[]);
app.controller('customersCtrl',函数($scope,$http){
$http.get(“results.php”)
.success(函数(响应){$scope.price=response.USD;});
});
还有最后一件事。你的ng repeat会一次又一次地重复同样的事情(如果你能让它这样工作的话),因为你在“x”中为每个值写同样的事情。也许考虑把它改为:
<div ng-app="myApp" ng-controller="customersCtrl">
<p>{{price['15m']}}, {{price['last']}}</p>
</div>
{{price['15m']},{{price['last']}
这一定是由于跨域请求造成的。你在控制台、chrome或fiddler的网络选项卡中看到错误了吗?@ShankarSangoli chrome控制台抛出以下错误:[$parse:syntax]…5m%20%2B%20'%2C%20'%20%2B%20x.last&p4=.15m%20%2B%20'%2C%20'%20%2B%20x.last at error(本机)at ib.thrower()at ib.parse()…将{x.15m+','+x.last}}
更改为{x[“15m”]+','+x.last}}以消除此错误。但是在你通过这个错误之后,你仍然会有跨域请求问题。不过“跨域请求问题”修复起来并不可怕。您可以启动服务器端数据请求。将其存储在本地文件中,当该请求返回时(在回调函数中),您可以将另一个请求发送到文件所在位置的您自己的服务器。此外。。。如果有帮助的话,请你接受我的回答好吗?