【问题标题】:Ajax with JQuery for Android Application with PhoneGap带有 JQuery 的 Ajax 用于带有 PhoneGap 的 Android 应用程序
【发布时间】:2012-04-20 13:50:36
【问题描述】:

我正在使用 PhoneGap 在 Android 中开发本机应用程序。我在 Visual Studio (c#) 中创建了一个 Web 服务,它将返回一个 XML 片段。所以我已将返回的 XML 值转换为 JSON,并在 Mozilla firefox 中对其进行了测试。但是当我尝试使用 AJax 和 Jquery 调用 Web 服务时。我尝试对将返回服务数据的警报消息进行编码。但是它似乎没有返回任何数据。有人可以帮我吗?谢谢

这是我的代码:

$.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");
    }
});

【问题讨论】:

  • 您的控制台是否出现任何错误?你能列出来吗?
  • 警报“错误”是我得到的。 -_-
  • 让它提醒 textStatus 和 errorThrown 而不仅仅是“error” 也尝试使用:console.log(errorThrown); console.log(textStatus);
  • 仍然无法从我的服务中得到任何响应。还尝试了答案 Authman Apatira。还是不行。 :(,你有什么我可以看的参考吗?试过谷歌搜索,有很多建议。但我想试试有人已经体验过的。

标签: jquery android ajax


【解决方案1】:

尝试添加此代码:

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

只要您的网络服务不是本地托管的,您就应该可以正常访问它,请回复我的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);
}

【讨论】:

  • 实际上我的网络服务目前是本地托管的。这就是我使用 10.0.2.2:49878 地址的原因。无论如何。将查看您给我的链接。非常感谢。
  • 本地托管,我的意思是在同一设备上发出请求(localhost / 127.0.0.1 / 物理 android 设备本身)
猜你喜欢
  • 2014-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 2013-11-12
  • 1970-01-01
  • 2015-09-16
  • 1970-01-01
相关资源
最近更新 更多