【问题标题】:jQuery AJAX getJSON() Method example isn't working for mejQuery AJAX getJSON() 方法示例对我不起作用
【发布时间】:2014-04-05 01:44:48
【问题描述】:

我直接从 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>

【问题讨论】:

标签: jquery ajax getjson


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您需要将jquery/demo_ajax_json.js 替换为your file name.js

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      相关资源
      最近更新 更多