简体   繁体   English

无法在iPhone的PhoneGap中使用AJAX(简单javascript)调用基于REST的Web服务

[英]Not able to call REST based web-service using AJAX(simple javascript) in PhoneGap for IPhone

Plz help, 请帮助

I am simply calling the Rest Based Web-Service in my new PhoneGap App for Iphone. 我只是在新的iPhone版PhoneGap应用中调用基于Rest的Web服务。

I am using the XCode 3.2.3 in which I have installed the PhoneGap framework and it works fine. 我正在使用XCode 3.2.3,其中已经安装了PhoneGap框架,并且工作正常。

Developed a simple javascript code which uses the AJAX. 开发了使用AJAX的简单javascript代码。 Below is the code. 下面是代码。

THis code works fine the in: IE, Morzilla and Safari Browser of my Mac Pc also on Safari browser of the Iphone Simulator but when I integrate this code in my PhoneGap app's index.html in www folder it doesn't give any response. 该代码可以在以下环境中正常工作:Mac PC的IE,Morzilla和Safari浏览器也可以在Iphone Simulator的Safari浏览器中使用,但是当我将此代码集成到www文件夹中PhoneGap应用的index.html中时,它不会给出任何响应。

<html>
<head>
<script language="javascript" type="text/javascript">


    function callingRestBasedWebService()
    {

        alert('In callingRestBasedWebService');

        var username = document.getElementById('usernameid').value;
        var password = document.getElementById('passwordid').value;
        var xmlhttp;
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    var responseXML = xmlhttp.responseText;
                alert("ResponseXML="+responseXML);

                if (window.DOMParser)   //Creating DOM Object for Morzilla  for parsing XML
                {
                    parser=new DOMParser();
                    xmlDoc=parser.parseFromString(responseXML,"text/xml");
                }
                else // Internet Explorer   // Creating DOM object for IE for parsing XML
                {
                    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                    xmlDoc.async="false";
                    xmlDoc.loadXML(responseXML);
                }

                    x=xmlDoc.getElementsByTagName("Login");
                    if(x[0].getElementsByTagName("status")[0].childNodes[0].nodeValue == 'Success')
                    {
                        alert('Login Successfull');

                    }else {
                        alert('Login Failed');
                    }


                }
        }

        xmlhttp.open("GET","http://pc-a401115.patni.com:8080/BankWebService/resources/validateLogin?username="+username+"&password="+password,true);
        xmlhttp.send();
    }




</script>
</head>
<body>

<br><br>
Username : <input type="text" name="username" id="usernameid"><br><br>
Password :<input type="text" name="password" id="passwordid"><br><br>
<input type="button" name="Login" value="Login" onClick="javascript:callingRestBasedWebService();">
</body>
</html>

Could someone help me out, and plz inform if I am wrong some where.. 有人可以帮助我吗,请在某些地方告诉我是否有误。

Thanks, MobileAppMaster 谢谢,MobileAppMaster

since "pc-a401115.patni.com" has no dns record I suspect that your iphone can not resolve the hostname which should lead to a very long timeout (properly one minute with phonegap) 由于“ pc-a401115.patni.com”没有DNS记录,我怀疑您的iPhone无法解析主机名,这将导致非常长的超时(使用phonegap大约一分钟)

Try using the ip address of your webserver instead. 请尝试使用您的网络服务器的IP地址。

If you use the latest version of phonegap, insert after head: 如果您使用最新版本的phonegap,请在头后面插入:

<script type="text/javascript" charset="utf-8" src="phonegap.0.9.5.1.min.js">
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM