0

我正在尝试使用 wsf.cdyne.com/WeatherWS/Weather.asmx 提供的天气 Web 服务。我确信我可以通过使用 uri "' http://wsf.cdyne.com/WeatherWS/Weather.asmx/GetCityForecastByZIP?ZIP= ' + zipcode" 获得 XML 格式的响应。

所以我现在要做的是使用 XmlHttpRequest 发送上面的 uri。我添加了一些警报来监控状态。在 open() 之后,readyState 为 1。之后我无法得到任何其他响应。如果我删除语句“xmlHttpRequest.onreadystatechange = processRequest;”,我在 send() 之后看不到任何响应。所以我只是希望有人能帮我检查一下有什么问题。

    <html>  
    <head>  
    <title>weather app</title>  
    </head>  
    <body>
        <script language="JavaScript">
        function httpGet()
        {
            var xmlHttp;  
            if (window.XMLHttpRequest) {  
                xmlHttp = new XMLHttpRequest();  
                if (xmlHttp.overrideMimeType)  
                    xmlHttp.overrideMimeType('text/xml');  
            } 
            else if (window.ActiveXObject) {  
                try {  
                    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");  
                } 
                catch (e) {  
                    try {  
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
                    } 
                    catch (e) { 
                    }
                }  
            }  

            xmlHttp.open( "GET", "http://wsf.cdyne.com/WeatherWS/Weather.asmx/GetCityForecastByZIP?ZIP=85281", false );
            alert("1 " +xmlHttp.readyState);
            xmlHttpRequest.onreadystatechange = processRequest;
            alert("2 " +xmlHttp.readyState);
            xmlHttp.send();
            alert("3 " +xmlHttp.readyState);


            document.write(xmlHttp.responseText);
            return xmlHttp.responseText;
        }

        httpGet();

        </script>
    </body>  
    </html>                 
4

1 回答 1

1

正如@robertklep 正确指出的那样,此请求是跨域的。浏览器不允许跨浏览器请求作为安全措施,因此您不会劫持用户在其网站上的会话等。

要让它工作,您可以在本地站点上创建一个代理。如果该站点支持使用 JSONP 跨域,您可以使用它。

有关更多信息,请查找有关跨域策略的一些信息,或者如果他们有一些 API 文档,他们可能也有关于您的问题的信息。

于 2013-03-16T19:55:37.300 回答