Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JSONP从Yahoo Finance加载报价_Javascript_Jquery_Callback_Jsonp - Fatal编程技术网

Javascript 使用JSONP从Yahoo Finance加载报价

Javascript 使用JSONP从Yahoo Finance加载报价,javascript,jquery,callback,jsonp,Javascript,Jquery,Callback,Jsonp,我希望这会这么容易。但不,警报从未被调用。请帮忙 $.getJSON("http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&view=basic&callback=?", function(result){ //response data are now in the result variable alert(result); }); 我试了一下公认的答案 但

我希望这会这么容易。但不,警报从未被调用。请帮忙

$.getJSON("http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&view=basic&callback=?", function(result){
   //response data are now in the result variable
   alert(result);
});
我试了一下公认的答案 但这对我也不起作用:(

我从中赚了一笔,但运气不好

var quote;
$(".price").text("please");
$(document).ready(function() {
    $.ajax({
        url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%3D%22AAPL%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=quote",
        dataType: "jsonp",
        jsonp: "callback",
        jsonpCallback: "quote"
    });

    quote = function(data) {
        $(".price").text("$" + data.query.results.quote.AskRealtime);
    };
});

我发现ajax调用不起作用的原因是,查询将自动为您在URL末尾添加时间戳,以确保从不缓存ajax请求。Yahoo Finance web服务不支持timestamp参数

需要添加以下行:

$.ajaxSetup({'cache':true});
以下是完整的代码:

var quote;
$.ajaxSetup({'cache': true});
$(document).ready(function () {
    $.ajax({
        url: "http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&view=basic",
       dataType: "jsonp",
       jsonp: "callback",
       jsonpCallback: "quote"
    });

    quote = function (data) {
        var arrayLength = data.list.resources.length;
        var restext = '';
        for (var i = 0; i < arrayLength; i++) {
            restext += "<br>" + data.list.resources[i].resource.fields.name;
        }
        $('#res').html(restext);
    };
});
var报价;
$.ajaxSetup({'cache':true});
$(文档).ready(函数(){
$.ajax({
url:“http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&view=basic",
数据类型:“jsonp”,
jsonp:“回调”,
jsonpCallback:“报价”
});
quote=函数(数据){
var arrayLength=data.list.resources.length;
var restext='';
对于(变量i=0;i”+data.list.resources[i].resource.fields.name;
}
$('#res').html(restext);
};
});

我也让它在纯JS中工作

function getQuote(obj) {
    var arrayLength = obj.list.resources.length;
    for (var i = 0; i < arrayLength; i++) { 
        document.getElementById("ip").innerHTML += "<br>" + obj.list.resources[i].resource.fields.name;
    }
}
var url = "http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&view=basic&callback=getQuote"
var script = document.createElement('script');
script.setAttribute('src', url);
document.getElementsByTagName('head')[0].appendChild(script);
函数getQuote(obj){
var arrayLength=obj.list.resources.length;
对于(var i=0;i”+obj.list.resources[i].resource.fields.name;
}
}
变量url=”http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&view=basic&callback=getQuote"
var script=document.createElement('script');
script.setAttribute('src',url);
document.getElementsByTagName('head')[0].appendChild(脚本);

我发现ajax调用不起作用的原因是,查询将自动为您在URL末尾添加时间戳,以确保从不缓存ajax请求。Yahoo Finance web服务不支持timestamp参数

需要添加以下行:

$.ajaxSetup({'cache':true});
以下是完整的代码:

var quote;
$.ajaxSetup({'cache': true});
$(document).ready(function () {
    $.ajax({
        url: "http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&view=basic",
       dataType: "jsonp",
       jsonp: "callback",
       jsonpCallback: "quote"
    });

    quote = function (data) {
        var arrayLength = data.list.resources.length;
        var restext = '';
        for (var i = 0; i < arrayLength; i++) {
            restext += "<br>" + data.list.resources[i].resource.fields.name;
        }
        $('#res').html(restext);
    };
});
var报价;
$.ajaxSetup({'cache':true});
$(文档).ready(函数(){
$.ajax({
url:“http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&view=basic",
数据类型:“jsonp”,
jsonp:“回调”,
jsonpCallback:“报价”
});
quote=函数(数据){
var arrayLength=data.list.resources.length;
var restext='';
对于(变量i=0;i”+data.list.resources[i].resource.fields.name;
}
$('#res').html(restext);
};
});

我也让它在纯JS中工作

function getQuote(obj) {
    var arrayLength = obj.list.resources.length;
    for (var i = 0; i < arrayLength; i++) { 
        document.getElementById("ip").innerHTML += "<br>" + obj.list.resources[i].resource.fields.name;
    }
}
var url = "http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&view=basic&callback=getQuote"
var script = document.createElement('script');
script.setAttribute('src', url);
document.getElementsByTagName('head')[0].appendChild(script);
函数getQuote(obj){
var arrayLength=obj.list.resources.length;
对于(var i=0;i”+obj.list.resources[i].resource.fields.name;
}
}
变量url=”http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&view=basic&callback=getQuote"
var script=document.createElement('script');
script.setAttribute('src',url);
document.getElementsByTagName('head')[0].appendChild(脚本);

您在fiddle中犯的错误有两件事:当fiddle使用
https
时,通过
http
访问框架,并且没有在头中加载脚本(这给jsonp访问全局
查询
对象带来了麻烦)

养成使用
/
而不是
http
https
访问脚本的习惯,浏览器将为您选择正确的协议:

var quote;
$(".price").text("please");
$(document).ready(function() {
    $.ajax({
        url: "//query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%3D%22AAPL%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=quote",
        dataType: "jsonp",
        jsonp: "callback",
        jsonpCallback: "quote"
    });

    quote = function(data) {
        $(".price").text(data.query.created);
    };
});

您在fiddle中犯的错误有两件事:当fiddle使用
https
时,通过
http
访问框架,并且没有在头中加载脚本(这给jsonp访问全局
查询
对象带来了麻烦)

养成使用
/
而不是
http
https
访问脚本的习惯,浏览器将为您选择正确的协议:

var quote;
$(".price").text("please");
$(document).ready(function() {
    $.ajax({
        url: "//query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%3D%22AAPL%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=quote",
        dataType: "jsonp",
        jsonp: "callback",
        jsonpCallback: "quote"
    });

    quote = function(data) {
        $(".price").text(data.query.created);
    };
});

当您访问该JS FIDLE URL时,可能会在浏览器的错误控制台中看到重复的错误消息。错误消息只是因为使用了https。将链接更改为,错误消息将消失,但始终不会调用回调:(当您访问JS Fiddle URL时,查看浏览器错误控制台中收到的错误消息可能重复。错误消息只是因为使用了https。将链接更改为,错误消息消失,但仍然从未调用回调:(虽然这可能是一个很好的一般提示,但我看不出这如何回答您最初发布的问题。虽然这可能是一个很好的一般提示,但我看不出这如何回答您最初发布的问题。