xmlhttprequest GET in javascript does not give response

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-16 18:37:13

问题


I am trying to consume the weather web service provided by wsf.cdyne.com/WeatherWS/Weather.asmx. I am sure that I can get a response in XML format by using the uri " 'http://wsf.cdyne.com/WeatherWS/Weather.asmx/GetCityForecastByZIP?ZIP=' + zipcode".

So what I want to do now is sending the uri above using XmlHttpRequest. I added some alerts to monitor the status. After open() the readyState is 1. After that I can't get any other response. If I remove the statement "xmlHttpRequest.onreadystatechange = processRequest;", I cannot see any response after send(). So I just hope someone can help me to check what is wrong.

    <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>                 

回答1:


As correctly stated by @robertklep this request is cross-domain. Browsers disallow cross-browser requests as a security measure so you don't hijack the user's sessions on their sites etc.

To get it to work you can create a proxy on the local site. If the site offers support to use JSONP cross-domain, you could use that.

For more information lookup some information on cross-domain policies or if they have some API docs, they may have information there on your problem too.



来源:https://stackoverflow.com/questions/15453910/xmlhttprequest-get-in-javascript-does-not-give-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!