WCF + Json = wrong serialization

Why is this WCF 3.5 method

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Json
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    public string Upper(string text)
    {
        return text.ToUpper();
    }
}

returns {"d":"TEXT"}?

He must return {"TEXT"}

I invoke using jQuery.

    $("#upper").click(function() {
        $.ajax({
            type: "GET",
            url: "/Json.svc/Upper?text="+$("#input1").val(),
            success: function(data) {
                $("#input1").val(data.d);
            }
        });
    });
+3
source share
3 answers

This is a security feature that has been added to JSON serialization in .NET 3.5. This is a container object, so instead of, say, results[0]you just say results.d[0]. Read more in this article .

+8
source

I assume that you are using <enableWebScript/>behavior in your configuration, replace it with <webHttp defaultOutgoingResponseFormat="Json"/>, and you will get json without the root "d" and without the props "__type".

, 4.0

.

+1

Have you tried changing the BodyStyle property of your [WebGet] attribute so that the responses are not wrapped?

0
source

Source: https://habr.com/ru/post/1731076/


All Articles