我使用下面的代碼做YQL的跨域AJAX請求:
得到什麼時做跨域AJAX請求與YQL
function requestCrossDomain(site, callback) {
function cbFunc(data) {
// If we have something to work with...
alert("inside call back");
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.');
}
// If no url was passed, exit.
if (!site) {
alert('No site was passed.');
return false;
}
// 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=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=xml&callback=cbFunc';
// Request that YSQL string, and run a callback function.
// Pass a defined function to prevent cache-busting.
$.getJSON(yql, cbFunc);
console.log("outside call back");
}
,並呼籲上述如下:
requestCrossDomain('http://www.cnn.com', function(results) {
alert(results);
});
當我在firefox中運行上面的代碼時,儘管響應(在firebug控制檯中)顯示網站內部回調函數(cbFunc)的內容,但它沒有顯示任何警告。
第5行的console.log("inside call back")
的結果也不是在螢火蟲控制檯中打印。
任何人都可以建議我在哪裏出錯或上面有任何解釋嗎?
http://tek-insight.blogspot.in/2010/05/cross-domain-ajax-request-proxy-json.html http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-cross-domain-ajax-request-with-yql-and-jquery/ 相關計算器問題,可能的解釋:
BTW我通過已經走了。
它顯示在控制檯「錯誤」和「完整」。你能解釋爲什麼嗎? –
這意味着響應沒有返回成功頭與200代碼,但確實返回了其他東西。我修改了上面的答案,現在包含您可以檢查的回調中的其他字段。 – moka
它顯示:「error parsererror SyntaxError {}」 –