How to remove d: and __type from JSON response for ASP web service

前端 未结 3 883
别跟我提以往
别跟我提以往 2021-01-03 02:05

I\'ve found several solutions for this on the web that are for WCF web service and not ASP web service.

Currently, I\'m getting back a JSON response that says:

相关标签:
3条回答
  • 2021-01-03 02:32

    If you're using ServiceStack.Text JSON Serializer you just need to:

    JsConfig.ExcludeTypeInfo = true;
    

    This functionality was automatically added back in v2.28, but the code above keeps that out of the serialization. You can also change this behavior by Type with:

    JsConfig<Type>.ExcludeTypeInfo = true;
    
    0 讨论(0)
  • 2021-01-03 02:42

    I don't think that you can. I think that is the format of the json that is returned.

    You can try get rid of the ResponseFormat bit and return a string and use

    JavaScriptSerializer ser = new JavaScriptSerializer(); 
    string json = ser.Serialize(objectClass);
    return json;
    

    Or even better use the JSON.Net libraries.

    Also look at How to not serialize the __type property on JSON objects for how to remove the __type bit.

    0 讨论(0)
  • 2021-01-03 02:46

    For remove "d" and "__type":

    .svc

    [ServiceContract]
    public interface ITestService
    {
        [OperationContract]
        [WebInvoke(Method = "POST",RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        List<TestDTO> GetAll();
    }
    

    .config

    <behaviors>
      <endpointBehaviors>
        <behavior name="DebugJSonBehavior" >
          <enableWebScript />
          <!--need set automaticFormatSelectionEnabled attribute -->
          <webHttp automaticFormatSelectionEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="DebugJSonBehavior" >
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    

    Js:

        $.ajax({
            type: "POST",
            url: _serviceUrl + "/TestService.svc/GetAll",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (dataret) { ... },
            error: function (xmlHttpRequest, textStatus, errorThrown) {... },
            complete: function () { ... }
        });
    
    0 讨论(0)
提交回复
热议问题