【发布时间】:2011-11-30 05:36:56
【问题描述】:
我一直在尝试给出的代码 How to use YQL to retrieve web results?
但它不起作用。
请给我一些其他建议或纠正该代码。
我只是在 page_load 上调用一个函数
<body onload = "load_content();">
在 load_content() 方法中,我必须获取其他网站的提要并将其显示在我的 HTML 页面上。
Load_Content 方法
var query = "select * from html where url='http://www.imdb.com/title/tt0123865/'";
// Define your callback:
var callback = function(data) {
console.log("DATA : " + data);
};
// Instantiate with the query:
var firstFeedItem = new YQLQuery(query, callback);
// If you're ready then go:
console.log("FEED : " + firstFeedItem.fetch()); // Go!!
函数 YQLQuery
function YQLQuery(query, callback)
{
this.query = query;
this.callback = callback || function(){};
this.fetch = function() {
if (!this.query || !this.callback) {
throw new Error('YQLQuery.fetch(): Parameters may be undefined');
}
var scriptEl = document.createElement('script'),
uid = 'yql' + +new Date(),
encodedQuery = encodeURIComponent(this.query.toLowerCase()),
instance = this;
YQLQuery[uid] = function(json) {
instance.callback(json);
delete YQLQuery[uid];
document.body.removeChild(scriptEl);
};
scriptEl.src = 'http://query.yahooapis.com/v1/public/yql?q='
+ encodedQuery + '&format=json&callback=YQLQuery.' + uid;
document.body.appendChild(scriptEl);
};
}
数据变量中没有任何内容
【问题讨论】:
-
请出示您的
load_content()功能码。 -
控制台是否出现任何错误?
-
回调函数没有获取任何数据。控制台只是显示未定义
标签: javascript html json yql