开发者

How can I write this YQL statement for jquery jsonp

开发者 https://www.devze.com 2023-01-19 01:56 出处:网络
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.

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 op开发者_Python百科en 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.


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


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消