【问题标题】:YQL JSON script not returning?YQL JSON 脚本没有返回?
【发布时间】:2012-04-13 20:14:03
【问题描述】:

我这里有一个脚本,几乎直接从this 复制而来。为什么下面列出的代码不返回任何内容?

ajax.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">

<html dir="ltr" lang="en-US">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Cross-Domain Ajax Demo</title>
    </head>
    <body>
        <div id="container">
            <form>
                <p><label>Type a URL:</label><input type="text" name="sitename" id="sitename"/></p>
                <p><input type="submit" name="submit" id="submit" value="Make Cross Domain Ajax request"</p>
            </form>
        </div>

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" charset="utf-8"></script>
        <script type="text/javascript" src="cross-domain-requests.js"></script>

        <script type="text/javascript">
            $('form').submit(function() {
                var path = "www.google.com";
                requestCrossDomain(path, function(results) {
                    $('#container').html(results);
                });
                return false;
            });
        </script>
    </body>
</html>

cross-domain-requests.js:

// Accepts a URL and a callback function to run.
function requestCrossDomain( site, callback ) {

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

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

(我对脚本和 Ajax 比较陌生,所以如果我做了任何愚蠢的事情,我提前道歉。)

【问题讨论】:

    标签: javascript jquery ajax json yql


    【解决方案1】:

    尝试将 var yql 中的回调更改为 callback=?和'from xml'的select语句是这样的:

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

    【讨论】:

    • 天哪,它有效,非常感谢。这为我节省了几个小时的睡眠时间,我熬夜想知道为什么它不起作用:D
    • 酷,我很高兴它成功了。请将答案标记为已接受以结束此问题。
    • 我必须再等一分钟才能做到这一点对不起,我想我对堆栈溢出来说太新了编辑:对不起,我的意思是,它不会让我将其标记为回答
    • 回调中的问号不是生成随机字符串吗? YQL 将无法以这种方式缓存任何内容,并且您会很快消耗掉您的请求率。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多