使用 javascript 以 json 格式从雅虎财经获取股票报价
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17245498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get stock quotes from yahoo finance in json format using a javascript
提问by Vinay Abhishek Manchiraju
I was trying to get stock quotes from yahoo api. My input to the query is only a stock ticker ( from a text field). On button click the background JavaScript method "getprice()" is called. I have a java script code that looks like this
我试图从雅虎 api 获取股票报价。我对查询的输入只是股票代码(来自文本字段)。单击按钮时,将调用后台 JavaScript 方法“getprice()”。我有一个看起来像这样的java脚本代码
function getprice()
{
var symbol = $('#stockquote').val();
var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22"+symbol+"%22)%0A%09%09&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json";
$.getJSON(url, function (json)
{
var lastquote = json.query.results.quote.LastTradePriceOnly;
$('#stock').text(lastquote);
});
}
$('#stock').text(lastquote);
Here "stock" is the text field where I want to display the LastTradePriceOnly for the given ticker.
这里的“股票”是我想显示给定代码的 LastTradePriceOnly 的文本字段。
I do not see any output turning up. Debugging also does not show up any errors. Can I get any suggestions with this issue?
我没有看到任何输出出现。调试也不会显示任何错误。我可以获得有关此问题的任何建议吗?
回答by Vlad Bezden
Try this.
试试这个。
function getData() {
var url = 'http://query.yahooapis.com/v1/public/yql';
var symbol = $("#symbol").val();
var data = encodeURIComponent("select * from yahoo.finance.quotes where symbol in ('" + symbol + "')");
$.getJSON(url, 'q=' + data + "&format=json&diagnostics=true&env=http://datatables.org/alltables.env")
.done(function (data) {
$('#result').text("Price: " + data.query.results.quote.LastTradePriceOnly);
})
.fail(function (jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
console.log('Request failed: ' + err);
});
}
HereI also added working example for you.
在这里,我还为您添加了工作示例。
回答by Daniel C. Deng
This is how it's done in AngularJS in case you need it:
这是在 AngularJS 中完成的方式,以防您需要它:
In your view:
在您看来:
<section ng-controller='StockQuote'>
<span>Last Quote: {{lang}}, {{lastTradeDate}}, {{lastTradeTime}}, {{lastTradePriceOnly}}</span>
</section><br>
In your controller: The stock symbol name is passed via $scope.ticker_name to service method 'getData.getStockQuote'.
在您的控制器中:股票代码名称通过 $scope.ticker_name 传递给服务方法“getData.getStockQuote”。
appModule.controller('StockQuote', ['$scope', 'getData',
function($scope, getData) {
var api = getData.getStockQuote($scope.ticker_name);
var data = api.get({symbol:$scope.ticker_name}, function() {
var quote = data.query.results.quote;
$scope.lang = data.query.lang;
$scope.lastTradeDate = quote.LastTradeDate;
$scope.lastTradeTime = quote.LastTradeTime;
$scope.lastTradePriceOnly = quote.LastTradePriceOnly;
});
}]);
In your service:
在您的服务中:
appModule.service('getData', ['$http', '$resource', function($http, $resource) {
// This service method is not used in this example.
this.getJSON = function(filename) {
return $http.get(filename);
};
// The complete url is from https://developer.yahoo.com/yql/.
this.getStockQuote = function(ticker) {
var url = 'http://query.yahooapis.com/v1/public/yql';
var data = encodeURIComponent(
"select * from yahoo.finance.quotes where symbol in ('" + ticker + "')");
url += '?q=' + data + '&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
return $resource(url);
}
}]);