JQuery $.getJSON 不是 运行

JQuery $.getJSON Not Running

我是 J Query 初学者。在 $.getJSON 函数方面需要一些帮助。我正在尝试使用此功能从外部服务器检索数据,但是当我 运行 它时,我什么也得不到。我经历过无数的教程。这是我的代码示例...

    <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        var url='https://api.flightstats.com/flex/airports/samples/v1/lts/Airport_response.json';
        $('#button').click(function(){
            $.getJSON(url,function(json){
                $("#results").append(json.airport.name);
            });     
        });
    });
    </script>


</head>
<body>
    <input type="button" id="button" value="Go">
    <div id="results"></div>

</body>

这里是 JSON 文件内容..

{
"airport":
        {
    "fs":"PDX",
    "iata":"PDX",
    "icao":"KPDX",
    "faa":"PDX",
    "name":"Portland International Airport",
    "street1":"7000 NE Airport Way",
    "city":"Portland",
    "cityCode":"PDX",
    "stateCode":"OR",
    "postalCode":"97218",
    "countryCode":"US",
    "countryName":"United States"
        }

        }

我只是想获取 'name' 内容。 $.getJSON 是正确的函数还是我应该使用 $.ajax 或 $.get? 谢谢

这里的问题是 .getJSON 无法 return url 的数据。

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.flightstats.com/flex/airports/samples/v1/lts/Airport_response.json. This can be fixed by moving the resource to the same domain or enabling CORS.

如果您控制网站 flightstats.com,您可以为此添加必要的 headers。如果您不这样做,您可能想要创建一个轻量级服务器端提取以使用适当的 CORS 响应显示必要的 json。

有关 CORS 的更多信息,我无法轻松地放在 Whosebug 页面中,请参阅 https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS