WebService WCF does not return the correct status code when changing the response header

I have a very simple WCF web service that I call on the client side using Ajax:

Here is my web service:

public string TestService() {
    throw new Exception();
    return "";
}

Here's what my client side Ajax call looks like:

var mySuccess = function(result,statuscode,xhr){
    alert('success');
}

var myFail = function(result,statuscode,xhr){
    alert('failure');
}

$.ajax({
    type: 'Post'
    contentType: "application/json; charset=utf-8",
    url: '../myService.svc/TestService',
    data: '',
    dataType: "json",
    success: mySuccess,
    error: myFail
});

This web service fails every time (as it should be!), Returning the status and code "500: Internal Server Error". The callback function "myFail" starts as it should.

However, if I change the response header in my web service, for example:

public string TestService() {
    WebOperationContext.Current.OutgoingResponse.Headers.Add("token", "1");
    throw new Exception();
    return "";
}

then the return status / code will be "200: OK", every time!

, "mySuccess" , - . , .

-, ?

+3
1

WebFaultException HTTP. : throw new WebFaultException("Invalid request received.", HttpStatusCode.BadRequest);
BadReuqest - 400.

0

All Articles