0

Possible Duplicate:
XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin

I'm developing weather application which is working fine in browsers. But when I try to deploy in my android phone it is not working fine and it is throwing error. XML response is null. please help me.

<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>
3
  • XMLHttpRequest cannot load webservicex.net/globalweather.asmx?op=GetWeather. Origin null is not allowed by Access-Control-Allow-Origin. Commented Jan 29, 2013 at 5:22
  • Uncaught TypeError: Cannot read property 'Body' of null Commented Jan 29, 2013 at 5:22
  • The above code is working fine in browsers after disabling browser security...but it is not working in physical devices..and I'm getting XML response is null.. Commented Jan 29, 2013 at 5:26

1 Answer 1

0

You need to allow the cross domain calls to http://www.webservicex.net

See this post: Cordova 1.9.0 Ajax not retrieving

Edit your xml in your res folder to include this line:

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

Or if its Phonegap-Build add that line to the config.xml

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.