I am trying to make an http call to a URL to get a JSON and display this on my page.
The JSON looks like this:
{"ticker":{"high":484.01099,"low":470.37201,"avg":477.1915,"vol":2193393.03322,"vol_cur":4588.62668,"last":482.16,"buy":482.16,"sell":481.2,"updated":1398350394,"server_time":1398350394}}
The code is included below. I read that I have to use 'JSONP' to but I couldn't figure out how to use it.
<html ng-app>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.6/angular.min.js"></script>
<div id = "content" style="min-width: 1200px; max-width: 90%: margin-left: auto; margin-right: auto;">
<div style="width: 520px; float: left;">
<h4>Bot Summary:</h4>
</div>
<div ng-controller="TickerControl" style="width: 680px; float: left;">
<h4>Market Summary</h4>
<p>Price = {{data}} </p>
</div>
</div>
<div></div>
<script type="text/javascript">
function TickerControl($scope, $http, $templateCache) {
$scope.method = 'JSONP';
$scope.url = 'https://btc-e.com/api/2/btc_usd/ticker?callback=JSON_CALLBACK';
$scope.getTicker = function() {
$scope.code = null;
$scope.response = null;
$http({method: $scope.method, url: $scope.url, cache: $templateCache}).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
}).
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});
};
$scope.getTicker();
}
</script>
</body>
</html>
UPDATE
I have now modified my code to try and do JSONP requests. I am getting the following error:
Resource interpreted as Script but transferred with MIME type text/html: "https://btc-e.com/api/2/btc_usd/ticker?callback=angular.callbacks._0". angular.js:8582
Uncaught SyntaxError: Unexpected token : ticker:1
I seem to be getting text back. Since I cannot control the server response, how can I parse this text to JSON... or just even display it... It's available to view in the chrome dev environment.
UPDATE 2
Apparently this seems to be an issue with the server not being configured properly. Since I don't have access to it, it would be nice to be able to just receive text and parse it in the client!
Price = {{getTicker()}}
` is making continuous calls to a method that connects to the api, thats why it hangs, its like an infinite loop – Tim Apr 24 '14 at 13:03Price = {{getTicker()}}
` though – Tim Apr 24 '14 at 13:10Price = {{data}}
` or something similar instead, so it loads some data from the scope instead of calling the function over and over – Tim Apr 24 '14 at 13:14