本文介绍了YQL JSON脚本没有返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更直接关闭这个。为什么不code,下面列出,返回什么?

I have a script here, copied pretty much directly off this. Why doesn't the code, listed below, return anything?

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>

跨域-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,所以我提前道歉,如果我做任何愚蠢的事。)

(I'm relatively new to scripting and Ajax, so I apologise in advance if I do anything stupid.)

推荐答案

试着改变回调VAR YQL回调=?和SELECT语句从XML'是这样的:

Try changing the callback in var yql to callback=? and the select statement to 'from xml' like this:

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

这篇关于YQL JSON脚本没有返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 12:22