views:

68

answers:

1

I have an ASP.NET Ajax service set up using WebSriptServiceHostFactory in the *.svc file - no web.config configuration. In the contract, I'm starting with two very simple methods:

    [OperationContract()]
    [WebGet]
    string GetPersonalInformationLabel();

    [OperationContract()]
    [WebGet]
    string GetCorporateInformationLabel();

And my jQuery set-up is as follows:

     $.ajaxSetup({
        type: "POST",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        dataFilter: function(data){
            var msg;

            if(    typeof(JSON) !== 'undefined' &&
                typeof(JSON.parse) === 'function')
                msg = JSON.parse(data);
            else
                msg = eval('(' + data + ')');

            if(msg.hasOwnProperty('d'))
                return msg.d;
            else 
                return msg;
        }
    });

  $("#chkCorporateGift").click(function(){
   if($(this).is(":checked")){
    $.ajax({
     type: "GET",
     url: "http://localhost/Services/OG.svc/GetCorporateInformationLabel",
     success: function(msg){
      $("#lblInformationType").text(msg);
     }
    });
   }
   else {
    $.ajax({
     type: "GET",
     url: "http://localhost/Services/OG.svc/GetPersonalInformationLabel",
     success: function(msg){
      $("#lblInformationType").text(msg);
     }
    });
   }
  });

As you can see, ajaxSetup assigns type to be "POST" by default, but I had to override it with "GET" in my two calls below as I was getting "405 Method Not Allowed" probably because the contract uses [WebGet] attribute on both methods

So now that 405 message is gone, I go ahead and call the two methods directly in my browser and they return the expected results. However, nothing is returned when the two methods are called using the jQuery code I've set up above. Any ideas as to what I'm doing wrong?

A: 

The first thing I would do is check with Fiddler or Firebug whether anything is actually sent back to the browser. If that's the case, you can set a breakpoint inside your JavaScript methods using Visual Studio 2010 and check what is going on there.

You do actually see the web service methods getting hit when you call them from the client?

If this all seems to be working fine, I would temporarily take out the dataFilter function and see what happens. Looking at your code I think this is the only location where things could go wrong (although not sure what goes wrong). Do you actually get an empty return message or is the success handler never called?

Ronald Wildenberg
Yes, Firebug shows that they are getting hit with a status of 200.
alkos333
I added another suggestion...
Ronald Wildenberg
How's Fiddler different from Firebug for HTTP traffic analyzing though?
alkos333
They offer more or less the same functionality for traffic analyzation. Firebug offers a lot more client-side functionality. Fiddler can be installed into Internet Explorer and since you were working on an ASP.NET website, I assumed you were using IE.
Ronald Wildenberg
I have Firefox set up as my default browser, so everything is done using FF. Yes, I love Firebug b/c of all the functionality that it offers aside from traffic analysis.
alkos333