2

我直接从 w3 学校示例中获取了此代码,但是,在他们的“Tryit 编辑器”之外,它对我不起作用。有任何想法吗?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("button").click(function () {
                $.getJSON("http://w3schools.com/jquery/demo_ajax_json.js", function (result) {
                    $.each(result, function (i, field) {
                        $("div").append(field + " ");
                    });
                });
            });
        });
    </script>
</head>
<body>
    <button>Get JSON data</button>
    <div></div>
</body>
</html>
4

2 回答 2

1

您只能向当前域上的某个位置发出 AJAX 请求,除非您请求的是 JSONP 响应。这是所有浏览器中的安全功能。谷歌“同源策略”和“跨站点脚本”以获取更多信息。

解决方法是使用服务器端代理从外部域请求数据,然后使用 jQuery 查询本地代理。

于 2012-04-21T20:35:44.450 回答
0

您需要替换jquery/demo_ajax_json.jsyour file name.js

即, $.getJSON("http://w3schools.com/jquery/demo_ajax_json.js", function (result)$.getJSON("path/your file", function (result) {

于 2012-10-16T06:45:15.660 回答