Bill Young
Bill Young

Reputation: 139

Jquery Ajax to WCF : Problem posting data

I'm having problems passing a strongly typed object to WCF service via Jquery Ajax.

JQuery:

    var var1 = "test1";
    var var2 = "test2";
    var var3 = "test3";

    var myObject =
    {
        StatusRequest:
        {
            Var1: var1,
            Var2: var2,
            Var3: var3
        }
    };

    $.ajax({
        type: "POST",
        url: "UtilityService.svc/GetStatus",
        data: JSON.stringify(myObject),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function(data)
        {
            alert(data.d);
        }
    });

WCF:

    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    public string GetStatus(StatusRequest statusRequest)
    {
        return "hi";
    }

Class:

public class StatusRequest
{
    public string Var1
    {
        get;
        set;
    }
    public string Var2
    {
        get;
        set;
    }
    public string Var3
    {
        get;
        set;
    }
}

Fiddler:

{"StatusRequest":{"Var1":"test1","Var2":"test2","Var3":"test3"}}

I'm able to debug and watch the request come in but statusRequest is null.

Thanks

Upvotes: 0

Views: 1575

Answers (1)

Joe
Joe

Reputation: 82574

StatusRequest should be statusRequest

It needs to be the variable name not the class name.

Upvotes: 2

Related Questions