views:

581

answers:

2

Why 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"} ?

It should returns {"TEXT"}

I'm calling using jQuery.

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

Have you tried changing the BodyStyle property of your [WebGet] Attribute so that responses aren't wrapped?

tomasr
Yes, but I get this exception "The body style 'Wrapped' is not supported by 'WebScriptEnablingBehavior'. Change the body style to be 'WrappedRequest'."or"The body style 'Bare' is not supported by 'WebScriptEnablingBehavior'. Change the body style to be 'WrappedRequest'."
Zote
If you're using webScriptEnablingBehavior then yes, it can cause issues. Is there a particular reason you want that instead of webHttpBehavior?Also, have you checked this: http://www.west-wind.com/weblog/posts/324917.aspx
tomasr
No particular reason. I did yesterday like your link, but I think my web.config was wrong, so I got another tutorial on bing. That's tutorial are working fine, the only issue is this d "property".
Zote
+5  A: 

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

Brandon Montgomery
Nice Brandon, but can I remove/disable this?
Zote
Not that I am aware of. But why would you want to disable this? It's a big security risk if you disable it. As the article says, it's worth the effort now to deal with the "d" container object.
Brandon Montgomery
Also, `{'Text'}` isn't valid JSON. In jQuery 1.4.x, that will cause trouble, so the .d is nice to have for that reason too.
Dave Ward