AJAX请求中不存在"access-control-allow-origin"标头

onT*_*net 1 javascript ajax jquery

我使用以下URL获取天气应用程序的数据.

http://wsf.cdyne.com/WeatherWS/Weather.asmx/GetCityWeatherByZIP

我之前使用过这个API,但这次我正在尝试使用AJAX加载数据,因此在获取此数据时,我的页面的其余部分都不会重新加载.

这是我的javascript

<script>
    $(function () {
        var zip = 16001;

        $.ajax({
            type: 'GET',
            crossDomain:'true',
            url: 'http://wsf.cdyne.com/WeatherWS/Weather.asmx/GetCityWeatherByZIP' +zip,
            success: function (data) {
                console.log("Here is the data", data);
            },
            error: function () {
                alert("Error loading data");
            }
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

我一直在控制台中收到以下错误.

XMLHttpRequest cannot load http://wsf.cdyne.com/WeatherWS/Weather.asmx/GetCityWeatherByZIP16001. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:50733' is therefore not allowed access. The response had HTTP status code 500. weatherAjax.html:1
Run Code Online (Sandbox Code Playgroud)

这使得它们似乎是访问数据的问题.但我之前使用过这个用户输入邮政编码的简单表格.它返回了数据.现在唯一的区别是我想使用AJAX加载数据,因此整个页面不会重新加载.我究竟做错了什么?

Que*_*tin 7

以前您将用户的浏览器发送到其他人的网站.他们离开了你的网站,一切都很好.

现在,您正在尝试编写JavaScript以指示访问者的浏览器从其他人的网站(将使用他们拥有的任何auth/authz凭据)获取数据,并将该数据提供给您的代码.

例如,由于其他网站可能是在线银行,因此除非您请求数据的网站明确允许您访问该网站,否则这是禁止的.

有关进一步阅读,请参阅同源策略HTTP访问控制(CORS).