Cannot read response from AngularJS $resource JSONP get from Yahoo Finance

心不动则不痛 提交于 2020-01-24 01:17:41

问题


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/

I need historical prices, which Google does not provide, but Yahoo does. I modified the above jsfiddle to this: http://jsfiddle.net/curt00/BqtzB/

Here's the 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 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?

I know that Jquery's getJSON can be used with this Yahoo query, but supposedly AngularJS' $resource is faster and more efficient.


回答1:


Use angular's $http service.

With function jsonp from angular service $http, it is quite easy to achieve.

Service

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;
        }
    };
});

Controller

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();
}​

I've created a working example on jsFiddle.




回答2:


As a angularjs noob, this fixed it for me.

Changed my jsonp from:

JSON_CALLBACK({"term_jg_1":{"query":"Julia.......

to

angular.callbacks._0({"term_jg_1":{"query":"Julia...



回答3:


Thanks to everybody for providing suggestions.

@asgoth's suggestion looks slick but I was not able to get it to work on my AngularJS web app.

I replaced $resource with $http.jsonp and now it works. Example:

var query = 'select * from csv where url=\'http://ichart.yahoo.com/table.csv?s=' + symbol + '&a=' + (date.month - 1) + '&b=' + date.day + '&c=' + date.year + '&d=' + (date.month - 1) + '&e=' + date.day + '&f=' + date.year + '&g=d&ignore=.csv\'';
var url = 'http://query.yahooapis.com/v1/public/yql?q=' + fixedEncodeURIComponent(query) + '&format=json&callback=JSON_CALLBACK';

    $http.jsonp(url, {timeout: 30000}).success(function(json) {
        var result = json.query.results.row;
        //  process result
    }


来源:https://stackoverflow.com/questions/13963192/cannot-read-response-from-angularjs-resource-jsonp-get-from-yahoo-finance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!