tags:

views:

59

answers:

2

Hi,

I am calling a simple method on WCF side using Jquery.

$.ajax({
         type: "POST",
         url: "MyService.svc/TestJSON",
         data:'{"BikeId":"2"}',
         //data: '{"BikeId":"'+ id + '"}',                 
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function(msg) {
             alert(msg);
         },
         error: GetFailedMessage
     });
     function GetFailedMessage(msg) {
         alert('Error message. ' + msg.status + ' ' + msg.statusText);
     }
 });

My simple TestJSON is [OperationContract]

public string TestJSON(string id)
{
    Bikes b = new Bikes();
    b.Description = "blah blah";
    b.Name = "DMX100";
    b.Make = "2010";
    b.ID = id;
    string bikeJson = JsonConvert.SerializeObject(b);
    return bikeJson;
}

I know that this method is called by using Breakpoint but the parameter "id" is null. What am I missing here? Thanks

+1  A: 

Your service is expecting a parameter named id, and in the client-side you are sending the value using BikeId as the parameter name.

Either, the parameter name in your TestJSON method signature to:

public string TestJSON(string BikeId) {/*...*/}

Or modify the data object in the client-side:

$.ajax({
  type: "POST",
  url: "MyService.svc/TestJSON",
  data: '{"id":"'+ id + '"}', // <------
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    alert(msg);
  },
  error: GetFailedMessage
});
CMS
A: 

I'm guessing a little bit, but perhaps the service is inferring the name of your data element as 'id', when the browser is passing it as 'BikeId'?

Bruce
Thanks, got u...:)