我一直在用phonegap在android中开发一个应用程序。任何人都可以帮助我如何调用我的 web 服务,通过我的 android 应用程序通过 AJAX 和 jquery 输出 JSON 字符串?非常感谢你的帮助。
我的 Jscript 中有这段代码:
$(document).bind("mobileinit", function(){
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
});
$.ajax({
type: "POST",
timeout: moneTimeout,
url: "http://10.0.2.2:49878/SampleProject.aspx?p_trxn_type=doLogin&p_phoneNumber="+phoneNumber,
error: function (xhr, status, errorThrown)
{
alert(xhr.status);
},
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");
}
});
这是我喜欢从我的 c# webservice 获取的 JSON 数组:
{"Contacts":{"Contact":{"@phoneNumber":"0002221111","@countryCode":"1","@typeCode":"73","@type":"Default, Mobile, DefaultContactNumber","@id":"04359c0dcca64638a5fcbb17b386ceed","Salutation":"Mr.","FirstName":"Tom","MiddleName":null,"LastName":"Cruise","NameSuffix":null,"PrimaryAddress":{"Street":{"#cdata-section":"3135 Teodoro Drive"},"Street2":{"#cdata-section":""},"City":{"#cdata-section":"Detroit"},"State":"MS","Country":"US","Postal":"48228"}}}
我仍然收到警报:错误,并且它不显示来自我的 c# webservice 的 JSON 字符串数组