0

可能重复:
XmlHttpRequest 错误:Access-Control-Allow-Origin 不允许 Origin null

我正在开发在浏览器中运行良好的天气应用程序。但是,当我尝试在我的 android 手机中进行部署时,它无法正常工作并且会抛出错误。XML 响应为空。请帮我。

<html>
 <head>
    <title>Calling Web Service from jQuery</title>
    <script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>
    <script type='text/javascript' src='xmlObject.js'></script>
    <script type='text/javascript' src='jquery-1.8.2.min.js'></script>
    <script type="text/javascript" src="json2.js"></script>  
    <script type="text/javascript">

        $(document).ready(function () {
            $("#btnCallWebService").click(function (event) {
                alert('click' + $("#cityName").val());

                var xmlhttp = new XMLHttpRequest();     
                xmlhttp.open("POST", "http://www.webservicex.net/globalweather.asmx?op=GetWeather",true);
                xmlhttp.onreadystatechange = function () 
                {
                    if (xmlhttp.readyState == 4) 
                    {   
                        var myXML=xmlhttp.responseXML;
                        alert("Response XML in getWeatherInformation : ");
                        alert(myXML);                                                                        

                        var json = XMLObjectifier.xmlToJSON(xmlhttp.responseXML);                                                
                        var body=JSON.stringify(json.Body[0]);
                        var result = json.Body[0].GetWeatherResponse[0].GetWeatherResult[0].Text;

                        var myXML2=XMLObjectifier.textToXML(result);                        
                        var json2 = XMLObjectifier.xmlToJSON(myXML2);                       
                        var body2=json2;
                        var location=body2.Location[0].Text;
                        var time=body2.Time[0].Text;
                        var temperature=body2.Temperature[0].Text;
                        var pressure=body2.Pressure[0].Text;
                        alert("location"+location+"..."+time+".."+temperature+".."+pressure);

                    }
                }
                xmlhttp.setRequestHeader("Content-Type", "text/xml");
                var xml ='<?xml version="1.0" encoding="utf-8"?>'+
                            '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
                            '<soap:Body>'+
                            '<GetWeather xmlns="http://www.webserviceX.NET">'+
                            '<CityName>'+ $("#cityName").val() +'</CityName>'+
                            '<CountryName>India</CountryName>'+
                            '</GetWeather>'+
                            '</soap:Body>'+
                            '</soap:Envelope>';

                alert("Request XML : ");
                alert(xml);
                xmlhttp.send(xml);  

                            });

        });



        function processSuccess(data, status, req, xml, xmlHttpRequest, responseXML) {
            alert('success' + status + ">>" +typeof $(req.responseXML));
            var myObj = new Array();
            $(req.responseXML)
             .find('GetWeatherResult')
                    .each(function(){
                        alert($(this));
                      myObj.push($(this)); 

                    });
            $(myObj).each(function(){
                var x = $(this).find('Location').text();
                alert('loc'+ x + $(this).find('Location'));
                var p = $(this).find('Location');
                for (var key in p) {
                        alert(key + " -> " + p[key]);
                    }
                });
        }

        function processError(data, status, req) {
            alert(req.responseText + " " + status);
            console.log(data);
            console.log(status);
            console.log(req);
        }  


    </script>
</head>
<body>
    <h3>
        Weather Report
    </h3>
    Enter your city name:
    <input id="cityName" type="text" />
    <input id="btnCallWebService" value="GetInformation" type="button" />

</body>
</html>
4

1 回答 1

0

您需要允许对http://www.webservicex.net的跨域调用

看到这篇文章: Cordova 1.9.0 Ajax not retrieving

编辑 res 文件夹中的 xml 以包含以下行:

<access origin="http://www.webservicex.net*"/>

或者,如果它的 Phonegap-Build 将该行添加到 config.xml

于 2013-01-29T11:02:25.787 回答