使用带有PhoneGap的JQuery for Android应用程序的Ajax

时间:2012-04-20 13:50:37

标签: jquery android ajax

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

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

尝试添加此代码:

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