2010-10-08 65 views
1

我想與YQL和JQuery做一些跨域的東西,但我有點麻煩讓這個yql查詢正常工作。我怎樣才能寫這個YQL語句爲jquery jsonp

好了,所以這是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包含到查詢中。

任何幫助都可以。

回答

1

好,我知道了......如果任何人都需要這樣的東西來看看:

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

功能requestCrossDomain(網站,回調){

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