我怎样才能为jquery jsonp编写这个YQL语句

发布于 2024-09-26 08:46:48 字数 671 浏览 5 评论 0原文

我正在尝试使用 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 包含到查询中。

任何帮助都可以。

I'm trying to do some cross domain stuff with YQL and Jquery but i'm having a little trouble with getting this yql query to work right.

Okay so this is the YQL statement i'm trying to get to work:

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"

Now the yql statement that I am trying to change to suit what i'm trying to do is:

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

From what i've read in order for me to be able to pick and choose css selectors I need to invoke an open data table.

I don't know what or how I can change the example yql statement to include the USE and AS into the query.

Any help would do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提赋 2024-10-03 08:46:48

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

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

function requestCrossDomain( site, callback ) {

// 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.');
}
}

Okay I got it... if anyone else needs something like this take a look:

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

function requestCrossDomain( site, callback ) {

// 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.');
}
}
提笔书几行 2024-10-03 08:46:48

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

see the below example of how to get the news from YQL JSONP

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文