最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calling finance.yahoo api using jquery - Stack Overflow

programmeradmin1浏览0评论

I want to send http request for fetching finance.yahoo stock data with url like : .csv?s=GAIL.NS+BPCL.NS+%5ENSEI&f=snl1hgp which returns a csv file. I want to read the response data and fill it in a listview using Javascript or JQuery mobile. None of the links I referred helped me.

I tried using the following code:

$.ajax({
    type: "GET",
    url: ".csv",
    data: "s=GAIL.NS+BPCL.NS+%5ENSEI&f=snl1hgp",
    dataType: "text/csv",
    success: function(data) {
        alert(JSON.stringify('data is :' + data));
    }
});

I get blank data as alert. Any sample code or useful link would be appreciated.

I want to send http request for fetching finance.yahoo stock data with url like : http://finance.yahoo./d/quotes.csv?s=GAIL.NS+BPCL.NS+%5ENSEI&f=snl1hgp which returns a csv file. I want to read the response data and fill it in a listview using Javascript or JQuery mobile. None of the links I referred helped me.

I tried using the following code:

$.ajax({
    type: "GET",
    url: "http://finance.yahoo./d/quotes.csv",
    data: "s=GAIL.NS+BPCL.NS+%5ENSEI&f=snl1hgp",
    dataType: "text/csv",
    success: function(data) {
        alert(JSON.stringify('data is :' + data));
    }
});

I get blank data as alert. Any sample code or useful link would be appreciated.

Share Improve this question edited Mar 26, 2013 at 13:12 dfsq 193k26 gold badges242 silver badges259 bronze badges asked Mar 26, 2013 at 12:26 AnilAnil 1,0289 silver badges20 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 2

I think that the problem is the request is cross domain. There is another question about this here:

Cross-Domain get CSV file

and another answer here :Yahoo JSONP Ajax Request Wrapped in callback function

and a working example here: Displaying ajax results from yahoo finance using underscore.js

Here is a working jsfiddle which makes a jsonp request to d.yimg. to get the data http://jsfiddle/gp6zL/

    YAHOO.Finance.SymbolSuggest.ssCallback = function (data) {
        alert(JSON.stringify(data));
    };
    var query;
    query = 'Google';
    if (query.length > 0) {

        $.ajax({
            type: "GET",
            url: "http://d.yimg./autoc.finance.yahoo./autoc",
            data: {
                query: query
            },
            dataType: "jsonp",
            jsonp: "callback",
            jsonpCallback: "YAHOO.Finance.SymbolSuggest.ssCallback",
        });
    }

I try to take jQuery out of the equation. The following code will work as long as you whitelist "finance.yahoo.".

var request = new XMLHttpRequest();
request.open("GET", "http://finance.yahoo./d/quotes.csv?s=GAIL.NS+BPCL.NS+%5ENSEI&f=snl1hgp", true);
request.onreadystatechange = function() {//Call a function when the state changes.
    if (request.readyState == 4) {
        if (request.status == 200 || request.status == 0) {
            console.log(request.responseText);
        }
    }
}
request.send();
发布评论

评论列表(0)

  1. 暂无评论