问题描述
我能够采用了棱角分明JS $资源与JSONP从谷歌财经获得的股票价格。这是如下所示:
I am able to get stock prices from Google Finance using Angular JS $resource with JSONP. This is shown here: http://jsfiddle.net/8zVxH/1/
我需要的历史数据,其中谷歌不提供,但雅虎一样。我修改了上面的jsfiddle这样:
I need historical prices, which Google does not provide, but Yahoo does. I modified the above jsfiddle to this: http://jsfiddle.net/curt00/BqtzB/
这里的code:
angular.module('app', ['ngResource']);
function AppCtrl($scope, $resource) {
var yqlURL="http://query.yahooapis.com/v1/public/yql?q=";
var dataFormat="&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
var symbol = 'GOOG';
var startDate = '2012-12-05';
var endDate = '2012-12-06';
var historical_query = yqlURL+"select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20%22"+ symbol +"%22%20and%20startDate%20%3D%20%22"+ startDate +"%22%20and%20endDate%20%3D%20%22"+ endDate +"%22"+ dataFormat;
$scope.yahooFinance = $resource(historical_query,
{callback:'JSON_CALLBACK'},
{get: {method:'JSONP', isArray: false}});
$scope.indexResult = $scope.yahooFinance.get();
}
它在浏览器控制台生成一条错误消息:
It generates an error message in the browser console:
GET的*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20%22GOOG%22%20and%20startDate%20%3D%20%222012-12-05%22%20and%20endDate%20%3D%20%222012-12-06%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys?callback=angular.callbacks._0 400(错误请求)
GET http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20%22GOOG%22%20and%20startDate%20%3D%20%222012-12-05%22%20and%20endDate%20%3D%20%222012-12-06%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys?callback=angular.callbacks._0 400 (Bad Request)
有谁知道如何得到这个工作?
Does anybody know how to get this to work?
我知道jQuery的的getJSON可以与此雅虎查询可以使用,但推测AngularJS'$资源是更快和更有效
I know that Jquery's getJSON can be used with this Yahoo query, but supposedly AngularJS' $resource is faster and more efficient.
推荐答案
使用角度的$ HTTP服务。
Use angular's $http service.
从角服务的$ HTTP的功能JSONP,这是很容易实现的。
With function jsonp from angular service $http, it is quite easy to achieve.
服务
VAR应用= angular.module('对myApp',[]);
var app = angular.module('myApp', []);
app.factory('service', function($q, $http) {
return {
getHistoricalData: function(symbol, start, end) {
var deferred = $q.defer();
var format = '&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=JSON_CALLBACK';
var query = 'select * from yahoo.finance.historicaldata where symbol = "' + symbol + '" and startDate = "' + start + '" and endDate = "' + end + '"';
var url = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(query) + format;
$http.jsonp(url).success(function(json) {
var quotes = json.query.results.quote;
// filter + format quotes here if you want
deferred.resolve(quotes);
});
return deferred.promise;
}
};
});
控制器
function Ctrl($scope, service) {
$scope.symbol = "GOOG";
$scope.items = [];
$scope.startDate = '2012-12-05';
$scope.endDate = '2012-12-06';
$scope.getData = function() {
$scope.items = [];
var promise = service.getHistoricalData($scope.symbol, $scope.startDate, $scope.endDate);
promise.then(function(data) {
$scope.items = data;
});
};
$scope.getData();
}
我创建的。
这篇关于无法读取AngularJS $资源响应JSONP从雅虎金融得到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!