2012-04-20 91 views
1

我正在用PhoneGap在Android中开发本机应用程序。并且我已经在Visual Studio(c#)中创建了一个Websevice,它将返回一个XML片段。所以我已经将返回的XML值转换为JSON,并在Mozilla Firefox中进行了测试。但是当我试图用JQuery来调用AJAX的Webservice时。我试着编码一个警报消息,它将返回服务的数据。但它似乎不返回任何数据。任何人都可以请帮助我吗?感谢使用PhoneGap的JQuery for Android应用程序的Ajax

这里是我的代码:

$.ajax({ 
    type: "POST", 
    url: "http://10.0.2.2:49878/a.aspx?p_trxn_type=doLogin&p_phoneNumber="+phoneNumber, 
    error: function (XMLHttpRequest, textStatus, errorThrown) 
    { 
     alert("error"); 
    }, 
    dataType : "json", 
    cache:false, 
    async:false, 
    success: function (ret) 
    { 
     try { 
      var jsonObj = eval('(' + ret + ')'); 
      alert(jsonObj.Contacts.Contact['@phoneNumber']); 
      alert(jsonObj.Contacts.Contact.LastName); 
      alert(ret.Contacts.Contact['@phoneNumber']) 
      alert(ret.Contacts.Contact.LastName); 
     } 
     catch(ex) { 
      alert(ex.message); 
     } 
     console.log(ret); 
     alert(ret.length); 
     alert(ret); 
     alert(typeof ret); 
     alert("success"); 
    } 
}); 
+0

您是否获得在控制台中的任何错误? 你能列出他们吗? – sirmdawg 2012-04-20 14:07:56

+0

警报“错误”是我得到的。 -_- – Newbie 2012-04-20 14:15:09

+0

它警告textStatus和errorThrown,而不是只是“错误” 也尝试使用: console.log(errorThrown); console.log(textStatus); – sirmdawg 2012-04-20 16:49:08

回答

0

你可以添加以下代码:

$(document).bind("mobileinit", function(){ 
    $.support.cors = true; 
    $.mobile.allowCrossDomainPages = true; 
}); 

只要你的web服务不是本地托管,你应该能够访问它就好了,重新我的question here on StackOverflow

务必也被定义为检查出http://jquerymobile.com/test/docs/api/globalconfig.html,尤其是http://jquerymobile.com/test/docs/pages/phonegap.html

最后,你的错误函数将更好的帮助您:

error: function (XMLHttpRequest, textStatus, errorThrown) 
{ 
    alert(textStatus + " " + errorThrown); 
} 
+0

其实我的web服务是本地托管的那一刻。使用10.0.2.2:49878地址,无论如何,都会查看你给我的链接,非常感谢 – Newbie 2012-04-20 14:54:35

+0

通过本地托管,我的意思是在同一台设备上,请求源自(localhost/127.0.0.1 /物理机器人设备本身) – 2012-04-21 02:26:23

相关问题