从Phonegap使用ASP.NET Web服务

嗨,我一直在尝试从phonegap-android应用程序中使用ASP.NET编写的Web服务,但我似乎在某处某处犯了一个错误。

值得一提的是,当我在eclipse的android模拟器上运行它时失败了。 我已经从网络浏览器尝试了相同的代码,它工作得很好。

这是Index.html中与问题相关的部分

/* Here i declare 'webServiceURL' which holds the path to the service that's hosted at 10.0.0.174 (WLAN ip of my computer). I use this instead of 127.0.0.1 because on a mobile phone localhost points to the phone itself. */ // Here i declare 'datos' which are the parameters sent to the web service method $.ajax({ url: webServiceURL + "InicioSesion", type: "POST", contentType: "application/json; charset=utf-8", data: JSON.stringify(datos), dataType: 'json', beforeSend: function() {$.mobile.loading('show')}, complete: function() {$.mobile.loading('hide')}, success: function (data, textStatus, jqXHR) { // Here i do stuff with 'data' }, error: function (jqXHR, textStatus, errorThrown) { // Here i print errors }, )}; 

访问源权限已添加到phonegap config.xml

  

对ASP.NET Web服务的web.config的修改

         

我面临的错误是,当我将’dataType’设置为’json’(这是我所期待的)时,ajax请求失败并且打印’textStatus’给我ParserERROR

所以我尝试使用’dataType’作为’text’而不是’json’来查看Web Service响应是否有问题,我意识到问题是响应是NULL。

记得我提到的,这个代码在Web浏览器上运行得很好,它在从android模拟器运行的phonegap应用程序上失败了。

如果任何有使用phonegap使用ASP.NET Web服务的经验的人都可以帮助我! 我不知道我错过了什么或做错了什么! 我一直在研究这个2天,我找不到解决方案!

我弄清楚了我犯的错误!

在添加到phonegap config.xml的访问源权限中,我放了:

  

这是不正确的!,正确的方法是在星号(’*’)之前加上一个点(’。’):

  

就是这样! 问题解决了!

您可能需要使用[System.Web.Script.Services.ScriptService]来装饰您的Web服务,以使其可供javascript客户端使用。

例:

 [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class dummyWebservice : System.Web.Services.WebService { [WebMethod()] public string HelloToYou(string name) { return "Hello " + name; } [WebMethod()] public string sayHello() { return "hello "; } } 

来源和其他信息/示例: http //vincenthomedev.wordpress.com/2009/02/10/consuming-an-aspnet-web-service-or-page-method-using-jquery/