PhoneGap的阿贾克斯request.responseText空,试图从雅虎股票信息链接csv文件 [英] Phonegap Ajax request.responseText empty, trying to get a csv file from a yahoo stock info link
问题描述
我只使用HTML,JavaScript和CSS开发的应用程序,因为我正在做它的PhoneGap(所以没有PHP或类似的东西)。我想开发使用雅虎股票CSV API一个简单的股票行情应用程序。
I'm developing an app using only html, javascript and css because I'm making it for phonegap (so no php or anything like that). I am trying to develop a simple stock quotes application that uses the yahoo stock csv api.
这个API的工作,向其提供任何给定股票的象征,它会下载一个CSV文件,该文件提供了有关股票的信息。
This api works by providing it the symbol of any given stock and it will download a csv file that gives you information about the stock.
这是例子是,如果你看看信息关于特斯拉汽车公司,其股票代码为TSLA,你会使用下面的链接:
An example would be if you look up info about Tesla Motors, its stock symbol is TSLA, and you would use the following link:
[http://download.finance.yahoo.com/d/quotes.csv?s=TSLA&f=nsl1op&e=.csv][1]
将这个链接手动,会自动下载与特斯拉汽车公司的信息csv文件,因此该文件的内容会是这个样子:
Going to this link manually, will automatically download the csv file with the info on Tesla Motors, so the file contents would look something like this:
"Tesla Motors, Inc","TSLA",84.35,81.23,83.24
但是,当我试图把它在我的应用程序,它返回一个空的响应。这里是我尝试获取信息:
But when I try to get it in my app, it returns an empty response. Here is how I try to get the info:
function getStockInfo(stockSymbol) {
var request = new XMLHttpRequest();
request.open("GET", "http://download.finance.yahoo.com/d/quotes.csv?s=" + stockSymbol + "&f=nsl1op&e=.csv", true);
request.send();
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200 || request.status == 0) {
alert("response: " + request.responseText);
var stockInfo = request.responseText.split(",");
localStorage.companyName = stockInfo[0];
localStorage.stockSymbol = stockInfo[1];
localStorage.startPrice = stockInfo[2];
localStorage.endPrice = stockInfo[3];
localStorage.highPrice = stockInfo[4];
document.location.href = "stockInfo.html";
}
}
}
}
行,上面写着警报(回应:+ request.responseText);
显示request.responseText是空白。我做这错了吗?如果它是一个链接,下载一个CSV文件,你必须让不同的要求?是否有这样做的PhoneGap的?我一直在试图找到这个答案,但没有设法的一种特殊方式。我想在这个AP preciate任何帮助。
The line that says alert("response: " + request.responseText);
shows the request.responseText is blank. Am I doing this wrong? If it's a link that downloads a csv file, do you have to get the request differently? Is there a special way of doing this for phonegap?, I've been trying to find the answer on this, but haven't managed to. I would appreciate any help on this.
推荐答案
如果你看的控制台,您将看到以下错误:
If you watch the console, you will see the following error :
XMLHtt prequest无法加载 http://download.finance。 yahoo.com/d/quotes.csv?s=TSLA&f=nsl1op&e=.csv 。 没有访问控制 - 允许 - 原产地标头的请求的present 资源。原产地'空',因此没有允许访问。
XMLHttpRequest cannot load http://download.finance.yahoo.com/d/quotes.csv?s=TSLA&f=nsl1op&e=.csv. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
您必须允许访问雅虎的服务器。在config.xml中添加行
You have to allow the access to the yahoo server. In config.xml add the line
<获得原产地=*/>
如果你想允许进入任何服务器或
<access origin="*" />
if you want to allow access to any server or
或&LT;获得原产地= HTTP://download.finance.yahoo.com/&GT;
如果你只希望允许访问雅虎的服务器
or <access origin=http://download.finance.yahoo.com" />
if you only want to allow access to the yahoo server
的http://docs.phonegap.com/en/edge/guide_appdev_whitelist_index.md.html#Whitelist%20Guide
这篇关于PhoneGap的阿贾克斯request.responseText空,试图从雅虎股票信息链接csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!