我正在尝试使用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!");
}
}
);
});
答案 0 :(得分: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) {