调用finance.yahoo api使用jquery [英] Calling finance.yahoo api using jquery

查看:274
本文介绍了调用finance.yahoo api使用jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送http请求获取finance.yahoo股票数据与url喜欢: http://finance.yahoo.com/d/quotes.csv?s=GAIL.NS+BPCL.NS+%5ENSEI&f=snl1hgp 返回一个csv文件。我想读取响应数据,并使用Javascript或JQuery移动在列表视图中填充它。



我尝试使用以下代码:

 

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

我得到空白数据作为警报。
任何示例代码或有用的链接将不胜感激。

解决方案

我认为问题是请求是跨域。此处还有另一个问题:



跨域获取CSV文件



还有另一个答案: Yahoo JSONP Ajax请求包含在回调函数中



href =http://stackoverflow.com/questions/14575323/displaying-ajax-results-from-yahoo-finance-using-underscore-js>使用underscore.js显示yahoo finance的ajax结果

这是一个工作的jsfiddle,它向d.yimg.com发送一个jsonp请求,以获取数据 http://jsfiddle.net/gp6zL/

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

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


I want to send http request for fetching finance.yahoo stock data with url like : http://finance.yahoo.com/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.com/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.

解决方案

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.com to get the data http://jsfiddle.net/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.com/autoc.finance.yahoo.com/autoc",
            data: {
                query: query
            },
            dataType: "jsonp",
            jsonp: "callback",
            jsonpCallback: "YAHOO.Finance.SymbolSuggest.ssCallback",
        });
    }

这篇关于调用finance.yahoo api使用jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆