雅虎JSONP AJAX请求包裹在回调函数 [英] Yahoo JSONP Ajax Request Wrapped in callback function

查看:413
本文介绍了雅虎JSONP AJAX请求包裹在回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白,我可以用jQuery,阿贾克斯和JSONP一个跨域Ajax调用。我打电话的雅虎股票报价API。一切工作,并将结果返回(我可以看到使用招。)问题是我得到一个js错误YAHOO是不确定的。我认为它有问题,因为JSON是在一个回调函数格式化因此其不正确的JSON语法。我能做些什么来解决这个问题?谢谢!这里是code:

  $。阿贾克斯({
            键入:GET,
            数据类型:JSONP,
            JSONP:'回调',
            jsonpCallback:YAHOO.Finance.SymbolSuggest.ssCallback,
            数据:{
                查询:request.term
            },
            网址:http://autoc.finance.yahoo.com/autoc,
            成功:功能(数据){
                警报(是);
            },
            错误:函数(XHR,ajaxOptions,thrownError){
                警报(xhr.status);
                警报(thrownError);
            }
        });
 

解决方案

我想添加这个答案,因为它看起来像 user209245的答案以上(这是自2011年)不再有效。以下是我做的:

  1. 使用的<一个href="http://developer.yahoo.com/yql/console/?q=show%20tables&env=store://datatables.org/alltableswithkeys#h=select%20%2a%20from%20yahoo.finance.quotes%20where%20symbol=%22AAPL%22">YQL控制台来打造你想要得到的股票,如查询苹果:

      

    从yahoo.finance.quotes选择*其中符号=AAPL

  2. 确保JSON被选中,并指定一个JSONP回调,如报价
  3. 点击测试
  4. 插件,它为您生成这样的REST查询:

     无功报价;
    
    $(文件)。就绪(函数(){
        $阿贾克斯({
            网址: "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",
            数据类型:JSONP
            JSONP:回调,
            jsonpCallback:引用
        });
    
        报价=功能(数据){
            $(代价)文本($+ data.query.results.quote.AskRealtime)。
        };
    });
     

    那么你的页面上的。价格 &LT; D​​IV&GT; 将显示:

      $ 543.21
     

当然,一旦你的数据备份可以显示任何你想要的;我只是用价格作为一个例子,因为这就是我需要这个了。

I understand that I can make a crossdomain ajax call with jquery, .ajax, and jsonp. I am calling the yahoo stock quote api. Everything is working and the result is returning (I can see using Fiddler.) The problem is I get a js error YAHOO is undefined. I think its having problems because the JSON is formated within a callback function so its not correct json syntax. What can I do to fix it? Thanks! Here is the code:

     $.ajax({
            type: 'GET',
            dataType: 'jsonp',
            jsonp: 'callback',
            jsonpCallback: 'YAHOO.Finance.SymbolSuggest.ssCallback',
            data:{
                query: request.term
            },
            url: 'http://autoc.finance.yahoo.com/autoc',
            success: function (data) {
                alert("yes");
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }
        });

解决方案

I wanted to add this answer since it looks like user209245's answer above (which is from 2011) no longer works. Here's how I did it:

  1. Use the YQL Console to build a query for the stock you want to get, e.g. Apple:

    select * from yahoo.finance.quotes where symbol="AAPL"

  2. Make sure JSON is selected and specify a JSONP callback, e.g. quote
  3. Click Test
  4. Plug in the REST query that it generates for you like this:

    var quote;
    
    $(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);
        };
    });
    

    Then on your page the .price <div> would display:

    $543.21
    

Of course, once you get the data back you can display anything you want; I'm just using price as an example since that's what I needed this for.

这篇关于雅虎JSONP AJAX请求包裹在回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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