简体   繁体   English

为什么我的jQuery / YQL调用不返回任何内容?

[英]Why does my jQuery/YQL call not return anything?

I'm trying to access YQL with jQuery but am not getting a response: 我正在尝试使用jQuery访问YQL,但没有得到响应:

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

Anyone know why? 有人知道为什么吗?

$(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!");
        }
    }
  );
 });

You need to leave off the quotes (since they're added already as part of the parameterization process), this: 您需要省略引号(因为它们已经作为参数化过程的一部分添加了),这是:

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

...which results in: ...导致:

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

should just be: 应该只是:

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

....which results in: ....导致:

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

Once you correct this, the format coming back is: 纠正此错误后,返回的格式为:

{"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"}}}}}

So you need a .query in there, like this: 因此,您需要在其中添加一个.query ,如下所示:

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

You can test it out here . 您可以在这里进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM