简体   繁体   English

使用YQL执行跨域AJAX请求时一无所获

[英]Getting nothing when doing cross domain AJAX request with YQL

I am using the following code to do the cross domain AJAX request with YQL : 我正在使用以下代码通过YQL进行跨域AJAX请求

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");

}

and calling the above as follow : 并按如下方式调用上述内容:
requestCrossDomain('http://www.cnn.com', function(results) {
alert(results);
});

When i am running the above code in firefox, although response (in firebug console) is showing the content of website inside callback function (cbFunc) yet it is showing nothing as alert. 当我在firefox中运行以上代码时,尽管响应(在firebug控制台中)正在回调函数(cbFunc)中显示网站的内容,但未显示任何警报。
Also the result of console.log("inside call back") at line 5 is not printing in firebug console. 另外,第5行中console.log("inside call back")的结果不在Firebug控制台中打印。

can anyone suggest me where things are going wrong or any explanation for above ? 谁能建议我哪里出了问题或上述解释?
btw i have already gone through : 顺便说一句,我已经经历过:
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/ Possible explanation in related stackoverflow questions. 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 /在相关的stackoverflow问题中的可能解释。

$.getJSON accepts as callback function for 'success' response. $ .getJSON接受作为“成功”响应的回调函数。 But if error were returned (404, 500, etc) then it will not call this function. 但是,如果返回了错误(404、500等),则它将不会调用此函数。
You need to add extra functions in order to catch other scenarios of responses: 您需要添加其他功能,以捕获其他响应方案:

$.getJSON( yql, cbFunc)
  .done(function() { console.log( "second success" ); })
  .fail(function(jqxhr, textStatus, error) { console.log( "error", textStatus, error ); })
  .always(function() { console.log( "complete" ); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM