POST parameter received is NULL in Restful service from a javascript ajax call

98 views Asked by At

Hi I'm pretty new to implementing Restful services. I researched on similar issues when doing POST here in stackoverflow and msdn but the solutions provided doesn't quite solve the problem I have.

Initially I was getting an error 400 (Bad Request) with the following. Notice that I have application/json in my contenttype as suggested in related posts.

function sendStartData() {
var gameID = global_GetCookie("gameID");
var eventID = global_GetCookie("eventID");
var urlVal = GetBaseURL()+"/event/"+eventID+"/start";
var dataPayload = {"gameID ":gameID,"timestamp":+new Date()};
var stringDataPayload = JSON.stringify(dataPayload);
$.ajax({
        type: 'POST',
        url: urlVal,
        contentType: "application/json; charset=utf-8",
        data: stringDataPayload,
        success: function (data) {},
        error: function (data) {},
        dataType: 'JSON'
});

}

then I read a post regarding bodystyle wrapped/bare so i then modified my datapayload to

var dataPayload = {"entity":{"someID ":someID,"timestamp":+new Date()}};

and now the service gets hit in visual studio and it doesn't return error 400 in the chrome console anymore. but the problem is that parameter is full of nulls eventhough I can see from javascript that it's filled with the correct values. I also tried adding BodyStyle = WebMessageBodyStyle.Wrapped into my webinvoke but then the entire 2nd parameter EventInfo newData became null instead of filled with null values. On the other hand added BodyStyle = WebMessageBodyStyle.Bare brings me back to error 400 (Bad Request)

My service function is below

    [OperationContract]
    [WebInvoke(Method = "POST",
               UriTemplate = "/event/{eventID}/start",
               RequestFormat = WebMessageFormat.Json,
               ResponseFormat = WebMessageFormat.Json)]
    void gameStarted(String eventID, EventInfo newData);

and my data contract class is

[DataContract]
public class EventInfo
{
    [DataMember]
    public int gameID { get; set; }

    [DataMember]
    public int timestamp { get; set; }
}

Any help is appreciated. I would just like my data to be received correctly in server side. Thanks in advance.

0

There are 0 answers