Get exception message when calling wcf service from jquery

I am calling a WCF service from jquery ajax. Sometimes the service throws some user errors, and when I need it, I need to get a message about this error. In the error function of my ajax call, I have the following code:

            error: function(data) {
              alert("responseText: " + data.responseText);
            }

And responseText, when an error occurs, looks something like this:

responseText: {"ExceptionDetail":{"HelpLink":null,"InnerException":null,"Message":"Denied",......}}

I want to get the "Message" from the "ExceptionDetail", but I'm not sure how to do this.

My WCF service looks like to check it out. I just throw an error:

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public string myFunction(string id)
{
    throw new Exception("Denied");
}

How can I get an error message?

+3
source share
2 answers

WCF - , .

, , :

 <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="debugBehavior">
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <services>
        <service name="YourService"
                 behaviorConfiguration="debugBehavior">
             .....
        </service>
    </services>

- , WCF , .NET → - .NET.

- (, , IErrorHandler SOAP - .NET. , .NET , .NET FaultException<T>, T - .NET.

, , FaultException<T>, .Detail .NET, ( .Message ).

+5

, , < serviceDebug includeExceptionDetailInFaults = "True" / > web.config marc_s :

<serviceBehaviors>
 <behavior name="">
  <serviceMetadata httpGetEnabled="true" />
  **<serviceDebug includeExceptionDetailInFaults="true" />**
</behavior>

jquery , , :

 .error(function (response, q, t) {
      var r = jQuery.parseJSON(response.responseText);
    });

, r.Message, .

: http://www.codegateway.com/2012/04/jquery-ajax-handle-exception-thrown-by.html

0

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


All Articles