【发布时间】:2015-08-09 20:24:10
【问题描述】:
我有这个脚本来获取从去年到今天的历史数据。该脚本在一只股票上运行良好(在本例中为“mo”)。我构建了一个数组,因为我想要 6 只股票,并且循环需要构建 6 个表。如果我用我的数组名称替换股票名称“mo”,我会得到错误。
var yyyy = new Date().getFullYear();
var mm = new Date().getMonth() + 1;
if (mm < 10) {
mm = "0" + mm;
}
var dd = new Date().getDate();
var endDate = yyyy + "-" + mm + "-" + dd;
var startDate = (yyyy - 1) + "-" + mm + "-" + dd;
$.ajax({
url: "https://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.historicaldata where symbol = 'mo' and startDate = '" + startDate + "' and endDate = '" + endDate + "'&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback=",
dataType: 'json',
success: function (data) {
var myTable = "";
myTable += "<br /><table border='1' cellpadding='0' cellspacing='0' width='500' bgcolor='green'>";
myTable += "<tr>";
myTable += "<td>Date</td><td>Open</td><td>Low</td><td>High</td><td>Volume</td><td>Close Price</td>";
myTable += "</tr>";
$.each(data.query.results.quote, function (index, item) {
myTable += "<tr><td>" + item.Date + "</td><td>" + item.Open + "</td><td>" + item.Low + "</td><td>" + item.High + "</td><td>" + item.Volume + "</td><td>" + item.Close + "</td></tr>";
});
myTable += "</table>";
$("#quotes").html(myTable);
},
error: function () {
$("#quotes").html('<p>Something has gone terribly wrong.</p>');
}
});
【问题讨论】:
-
数组代码在哪里?为什么不能对每一个提出要求?你试过在 YQL 控制台中调试吗?
标签: javascript jquery yql