2012-04-21 51 views
1

我直接从w3学校示例中获取此代码,但是,在“Tryit编辑器”之外,它不适用于我。有任何想法吗?jQuery AJAX getJSON()方法示例不适用于我

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

跨域限制,以取代jquery/demo_ajax_json.jsyour file name.js

$.getJSON("http://w3schools.com/jquery/demo_ajax_json.js", function (result)? – fmgp 2012-04-21 20:34:23

+3

此外,[避免使用w3schools](http://w3fools.com/)将是一个好主意。 – rid 2012-04-21 20:34:57

+0

http://www.ibm.com/developerworks/library/wa-aj-jsonp1/ – 2012-04-21 20:35:28

回答

1

只有当您请求JSONP响应时,才能向当前域上的某个位置发出AJAX请求。这是所有浏览器的安全功能。 Google针对“相同来源政策”和“跨网站脚本”获取更多信息。

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

0

您需要$.getJSON("path/your file", function (result) {

相关问题