Luis Crespo
Luis Crespo

Reputation: 1620

Consuming ASP.NET Web Service From Phonegap

Hi, i've been trying to consume a Web Service written in ASP.NET from a phonegap-android app but i seem to be making a mistake somehow somewhere.

It's worth mentioning that this fails when i run it on the android emulator from eclipse. I've already tried the same code from a web browser and it works just fine.

Here's the part of Index.html that's relevant for the question

/* 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
  },
)};

Access origin permission added to phonegap config.xml

<access origin="*"/>

Modifications to the web.config of the ASP.NET Web Service

<system.webServer>
  <httpProtocol>
   <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
   </customHeaders>
  </httpProtocol>
</system.webServer>

The error i'm facing is that when i set 'dataType' to 'json' (which is what i am expecting) the ajax request fails and printing 'textStatus' gives me ParserERROR.

So i tried using 'dataType' as 'text' instead of 'json' to see if there was something wrong with the Web Service response, and i realized that the problem was that the response was NULL.

REMEMBER what i mentioned, this code works perfectly on a Web Browser, it fails on the phonegap app running from the android emulator.

If anybody with a bit of experience using phonegap to consume an ASP.NET Web Service could please help me!. I have no idea of what i'm missing or doing wrong!. I've been working on this for 2 days by now and i just can't find a solution!

Upvotes: 3

Views: 2987

Answers (2)

Luis Crespo
Luis Crespo

Reputation: 1620

I've figured out the mistake i was making!.

On the access origin permission added to phonegap config.xml i was putting:

<access origin="*"/>

That's incorrect!, the right way to put it is with a dot ('.') before the asterisk ('*'):

<access origin=".*"/>

And that's it!, PROBLEM SOLVED!.

Upvotes: 3

ClintL
ClintL

Reputation: 1453

You may need to adorn your web service with [System.Web.Script.Services.ScriptService] to make it available to javascript clients.

Example:

[WebService(Namespace = "http://tempuri.org/&quot;)]  
[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 ";  
}    
} 

Source and Additional Info / Examples: http://vincenthomedev.wordpress.com/2009/02/10/consuming-an-aspnet-web-service-or-page-method-using-jquery/

Upvotes: 1

Related Questions