Tuesday, May 29, 2012

How to Get rid of __type and d in wcf json response

How to Get rid of __type and d in wcf json response


Change <enableWebScript /> to <webHttp /> in the web.config



Web.config 

<system.serviceModel>
    <behaviors>
       <serviceBehaviors>
         <behavior name="MyServiceTypeBehaviors" >
           <serviceMetadata httpGetEnabled="true" />
         </behavior>
       </serviceBehaviors>
       <!-- endpoint behavior -->
       <endpointBehaviors>
         < behavior>
           < webHttp />                   <-- add this
       <enableWebScript />    <-- remove that
         < /behavior>
       < /endpointBehaviors>
     < /behaviors>
   < /system.serviceModel>


Results:

with <enableWebScript />

{"d":[{"__type":"MethodName:#TestNamespace.Domain.ClassName","Date":"01","Name":"Tackling","Value":172},{"__type":"MethodName:#TestNamespace.Domain.ClassName","Date":"04","Name":"Tackling","Value":761},{"__type":"MethodName:#TestNamespace.Domain.ClassName","Date":"07","Name":"Tackling","Value":687},{"__type":"MethodName:#TestNamespace.Domain.ClassName","Date":"10","Name":"Tackling","Value":401}]}


with <webHttp />

[{"Date":"01","Name":"Tackling","Value":136},{"Date":"04","Name":"Tackling","Value":697},{"Date":"07","Name":"Tackling","Value":694},{"Date":"10","Name":"Tackling","Value":56}]

PS: The "__type" is some information given to the ASP.NET AJAX library to allow for round-tripping of objects from WCF to Javascript and back to WCF again.

PS: The d prefix is part of the response for the ASP.NET AJAX library to prevent attack

PS !Important:  you cannot use  if you are passing array's of objects through a parameter. Instead, add the  element to the binding.



No comments:

Post a Comment