【问题标题】:Why does my jQuery/YQL call not return anything?为什么我的 jQuery/YQL 调用不返回任何内容?
【发布时间】:2010-12-25 21:39:16
【问题描述】:

我正在尝试使用 jQuery 访问 YQL,但没有得到响应:

http://jsfiddle.net/tastyapple/grMb3/

有人知道为什么吗?

$(function(){
     $.extend(
         {
             _prepareYQLQuery: function (query, params) {
                 $.each(
                     params, function (key) {
                         var name = "#{" + key + "}";
                         var value = $.trim(this);
                         if (!value.match(/^[0-9]+$/)) {
                             value = '"' + value + '"';
                         }
                         query = query.replace(name, value);
                     }
                 );
                 return query;
             },
             yql: function (query) {
                 var $self = this;
                 var successCallback = null;
                 var errorCallback = null;

                 if (typeof arguments[1] == 'object') {
                     query = $self._prepareYQLQuery(query, arguments[1]);
                     successCallback = arguments[2];
                     errorCallback = arguments[3];
                 } else if (typeof arguments[1] == 'function') {
                     successCallback = arguments[1];
                     errorCallback = arguments[2];
                 }

                 var doAsynchronously = successCallback != null;
                 var yqlJson = {
                     url: "http://query.yahooapis.com/v1/public/yql",
                     dataType: "jsonp",
                     success: successCallback,
                     async: doAsynchronously,
                     data: {
                         q: query,
                         format: "json",
                         env: 'store://datatables.org/alltableswithkeys',
                         callback: "?"
                     }
                 }

                 if (errorCallback) {
                     yqlJson.error = errorCallback;
                 }

                 $.ajax(yqlJson);
                 return $self.toReturn;
             }
         }
     );

  $.yql(
    "SELECT * FROM github.repo WHERE id='#{username}' AND repo='#{repository}'",
    {
      username: "jquery",
      repository: "jquery"
    },
    function (data) {
        if (data.results.repository["open-issues"].content > 0) {
            alert("Hey dude, you should check out your new issues!");
        }
    }
  );
 });

【问题讨论】:

    标签: javascript ajax jquery yql


    【解决方案1】:

    您需要去掉引号(因为它们已作为参数化过程的一部分添加),这样:

    "SELECT * FROM github.repo WHERE id='#{username}' AND repo='#{repository}'"
    

    ...导致:

    SELECT * FROM github.repo WHERE id='"jquery"' AND repo='"jquery"'
    

    应该是:

    "SELECT * FROM github.repo WHERE id=#{username} AND repo=#{repository}"
    

    ....导致:

    SELECT * FROM github.repo WHERE id="jquery" AND repo="jquery"
    

    一旦你更正了这个,返回的格式是:

    {"query":{"count":"1","created":"2010-12-25T21:49:01Z","lang":"en-US","results":{"repository":{"url":"https://github.com/jquery/jquery","has-downloads":{"type":"boolean","content":"false"},"organization":"jquery","homepage":"http://jquery.com/","pushed-at":{"type":"datetime","content":"2010-12-25T09:56:56-08:00"},"created-at":{"type":"datetime","content":"2009-04-03T08:20:14-07:00"},"has-wiki":{"type":"boolean","content":"false"},"fork":{"type":"boolean","content":"false"},"forks":{"type":"integer","content":"496"},"private":{"type":"boolean","content":"false"},"open-issues":{"type":"integer","content":"35"},"name":"jquery","description":"jQuery JavaScript Library","watchers":{"type":"integer","content":"5387"},"owner":"jquery","has-issues":{"type":"boolean","content":"false"}}}}}
    

    所以你需要一个.query,像这样:

    if (data.query.results.repository["open-issues"].content > 0) {
    

    You can test it out here.

    【讨论】:

      猜你喜欢
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 2018-01-24
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多