jQuery AJAX getJSON() 方法示例对我不起作用

标签 jquery ajax getjson

我直接从 w3 学校的示例中获取了这段代码,但是,在他们的“Tryit Editor”之外,它对我不起作用。有什么想法吗?

<!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>

最佳答案

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

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

关于jQuery AJAX getJSON() 方法示例对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10262753/

相关文章:

javascript - Ajax - 将按钮值发送到 php

javascript - 来自另一个 Controller 的 Rails : Render a . js.erb?

javascript - 让 JSON 在 IE 中工作

javascript - IE6 中嵌套 JSON 会导致问题

javascript - 图像鼠标悬停下方的标题无法正常工作

jquery - 如何在选择listview jquery后显示警报

php - 使用 jQuery AJAX 出现语法错误

php - Jquery:如果有内容,只填充.load中的数据?

PHP - 检查请求的 SSL 证书

javascript - 捕获 $.getJSON 错误