Phonegap Ajax request.responseText empty, trying to get a csv file from a yahoo stock info link

徘徊边缘 提交于 2019-12-23 05:58:07

问题


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.

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.

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]

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

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.


回答1:


If you watch the console, you will see the following error :

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.

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

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



来源:https://stackoverflow.com/questions/21238139/phonegap-ajax-request-responsetext-empty-trying-to-get-a-csv-file-from-a-yahoo

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