WCF does not deserialize JSON. Parameters in the web service are zero.

I am having the same problems as in this question: WCF does not deserialize JSON input

I am perplexed and desperate for a solution. I read the answer network, but I only found this question that matches my exact problem. My datacontract parameter also doesn't work when the service starts.

I tried the answer points in the question above, but they don’t give me any hints (the web service does OK. I don't get any exceptions - and I don’t see what the deserializer is doing - or not doing as the case may be). I am completely new to WCF services, so please tell me about me.

I am using POST due to the size of the nested JSON.

Can any WCF master or original poster of this question please enlighten me?

If you need more detailed information, I have provided it upon request. My problem is so similar to this question that I was hoping the OP would be able to convey how it resolved the problem.

Updated

Code snippets - I'm under the NDA, so I can’t publish specifics.

"In-Page JS":

var settings = {
        a: $("#a").val(),
        b: $("#b").val(),
        c: $("#c").val(),
        d: $("#d").val(),
        e: $("#e").val(),
        f: $("#f").prop("checked").toString(),
        g: $("#g").prop("checked").toString()
    };

    var data= {
        a: [1011,1012,1013],
        b: JSON.stringify(settings),
        c: "01/01/2011 23:59:59"
    };

Library.Services.Post("path/to/service/Service.svc/SaveSettings", data, true, function (result) {

        if (result.Succeeded) {
            ShowSuccess("Success.");
        }
        else {
            ShowError("Failure.");
        }

    });

"Library.Services.Post":

Post: function (serviceUrl, data, async, successHandler, errorHandler) {
        var continueOperation = true;

        if (!data) {
            data = "{}";
        }

        try {
            var obj = $.parseJSON(data);
            obj = null;
        }
        catch (err) {
            continueOperation = false;
            JS.ShowError("Data attribute is not a valid JSON object");
        }

        if (typeof (data) !== "string") {
            data = JSON.stringify(data);
        }

        if (continueOperation) {
            Library.Services._ajax("POST", serviceUrl, data, async, successHandler, errorHandler);
        }

        continueOperation = null;
    }

"Library.Services._ajax":

_ajax: function (method, serviceUrl, data, async, successHandler, errorHandler) {

        if (!typeof (successHandler) === "function") {
            continueOperation = false;
            ShowError("Success handler must be a function");
        }

        try {
            $.ajax({
                async: async,
                cache: false, // don't cache results
                type: method,
                url: Library.Services.baseUrl + serviceUrl,
                contentType: "application/json; charset=utf-8",
                data: data,
                dataType: "json",
                processData: false, // data processing is done by ourselves beforehand
                success: function (data, statusText, request) {

                    if (data != null) {
                        if (data.d && data.d.__type) {
                            data = data.d;
                        }
                        else {
                            // Wrapped message: return first property
                            $.each(data, function (name, value) {
                                data = value;
                                return false;
                            });

                        }
                    }

                    successHandler(data, statusText, request);

                },
                error: function (request, statusText, error) {
                    //debugger;    
                    var res = request.responseText;

                    if (request.status != 200) {
                        if (!request.isResolved()) {
                            ShowError(request.status + " " + request.statusText);
                        }
                        else {
                            ShowError("Request could not be resolved.");
                        }
                    }
                    else {
                        ShowError("Unknown error status.");
                    }

                    if (typeof (errorHandler) === "function") {
                        errorHandler();
                    }

                }
            });
        }
        catch (err) {
            ShowError("AJAX call failed");
        }
    }

"Service.svc":

<DataContract()>
Public Class SaveSettingsContract
    <DataMember(Order:=1)>
    Public a() As String

    <DataMember(Order:=2)>
    Public b()() As String

    <DataMember(Order:=3)>
    Public c As String

End Class

<OperationContract()>
<WebInvoke(BodyStyle:=WebMessageBodyStyle.Wrapped,
        RequestFormat:=WebMessageFormat.Json,
        ResponseFormat:=WebMessageFormat.Json)>
Public Function SaveSettings(ByVal settings as SaveSettingsContract) As WebServiceResults.BaseResult

   ' At this point in debugging, settings is Nothing


End Function
+3
source share
2 answers

I believe that I have found the answer to this question.

I think the reason the parameter was empty was as follows:

var data= {
    a: [1011,1012,1013],
    b: JSON.stringify(settings),
    c: "01/01/2011 23:59:59"
};

should be as follows:

var data = {
    settings: {
        a: [1011,1012,1013],
        b: JSON.stringify(settings),
        c: "01/01/2011 23:59:59"
    }
}

, , settings, SaveSettings. , settings.

, , - , .

: , , asmx , , . WCF , .

+1

, , , , - .

( ), DataContractJsonSerializer . , , JSON ( Fiddler JSON), , , ( ).

+2

All Articles