1

我正在尝试使用 YQL 和 Jquery 做一些跨域的事情,但我在让这个 yql 查询正常工作时遇到了一些麻烦。

好的,这是我要开始工作的 YQL 语句:

use 'http://yqlblog.net/samples/data.html.cssselect.xml' as data.html.cssselect; select * from data.html.cssselect where url="www.holylandmoments.org/devotionals/the-sabbath-experience" and css="#main-content p"

现在我试图改变以适应我想要做的 yql 声明是:

var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=xml&callback=?';

从我读到的内容中,为了能够挑选和选择 css 选择器,我需要调用一个打开的数据表。

我不知道如何更改示例 yql 语句以将 USE 和 AS 包含到查询中。

任何帮助都可以。

4

2 回答 2

1

好的,我明白了...如果其他人需要这样的东西,请看一下:

// Accepts a url and a callback function to run.

函数请求跨域(站点,回调){

// Take the provided url, and add it to a YQL query. Make sure you encode it!
var yql = 'http://query.yahooapis.com/v1/public/yql?q=use' + "%20'http%3A%2F%2Fyqlblog.net%2Fsamples%2Fdata.html.cssselect.xml'%20as%20data.html.cssselect%3B%20" + encodeURIComponent('select * from data.html.cssselect where url="' + site + '"') + "%20and%20css%3D%22%23main-content%20p%22" + '&format=xml&callback=?';

// Request that YSQL string, and run a callback function.
// Pass a defined function to prevent cache-busting.
$.getJSON( yql, cbFunc );

function cbFunc(data) {
// If we have something to work with...
if ( data.results[0] ) {
    // Strip out all script tags, for security reasons.
    // BE VERY CAREFUL. This helps, but we should do more. 
    data = data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');

    // If the user passed a callback, and it
    // is a function, call it, and send through the data var.
    if ( typeof callback === 'function') {
        callback(data);
    }
}
// Else, Maybe we requested a site that doesn't exist, and nothing returned.
else throw new Error('Nothing returned from getJSON.');
}
}
于 2010-10-12T15:44:08.263 回答
1

请参阅下面的示例,了解如何从 YQL JSONP 获取新闻

http://www.techtricky.com/jquery-code-to-display-top-news-using-yql-jsonp-api-service/

于 2012-05-09T13:41:08.660 回答