Search code examples
javascriptjqueryiis-7wcf-rest

Ajax call restful WS is always getting error (fiddler gets the good response)


I have made Restfull WS in VS 2010 with two simple method (one GET, another POST). They look like:

[ServiceContract]
public interface IService1
    {

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "createUser")]
    string createUser();

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, UriTemplate = "loginUser/{email}/{password}")]
    string loginUser(string email, string password);

    }

Definition of these methods is simple:

public class Service1 : IService1
    {

 public string createUser()
        {
            return "Successful POST call !!! ";
        }

 public string loginUser(string email, string password)
        {
            return "Successful GET call !!! " + email + " - "+ password;
        }
  }

I have published this service to IIS and tested my method in browser (only loginUser (GET) method, cannot test createUser (POST) method by the browser) and method (loginUser ) work fine. When I tried to call method by jQuery AJAX, I am always getting error call without any notification. I checked my fiddler and there are the right response.

My Ajax method:

$(document).ready(function(){

$("#button2").click(function(){

  $.ajax({
   type: "GET",                 
   url: "http://localhost/AutoOglasi/Service1.svc/loginUser/bole/bole",

   success: function (response) {
    alert("respons  "+response);
   },
       error: function (request, status, error) {
    alert(request.responseText+" -- " + status + " --- "+ error);
   }
     });                

});

});

I mozila firebug i section XML I get this:

XML Parsing Error: no element found Location: moz-nullprincipal:{ba25ef4a-f215-486e-b965-e70714c5af31} Line Number 1, Column 1: ^

What I am doing wrong here, I just cannot figure out, because fiddler is giving me good response?


Solution

  • You state ResponseFormat = WebMessageFormat.XmlResponseFormat and what you return is not xml but a string. It expects the first character to be a < but its a S thats why its not finding an element at position 1