I have the following code:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static MyResponse myMethod(ParameterClass parameters)
{
MyResponse resp = new MyResponse();
resp.errors.Add("Some error message");
HttpContext.Current.Response.StatusCode = 400;
return resp;
}
running this with jQuery.ajax using url MyPage.aspx/myMethod this call does not return my payload from MyResponse object. Once i switch the statuscode to 200 the return works using the success callback in ajax, but i want to return custom error messages with 4xx and 5xx status codes, how to make it work.
It looks like IIS is the problem, but what do i have to do to still allow returning my class objects on error?
Update 1: i also watched network tab in chrome, IIS is not returning the payload at all, so jQuery cannot be the problem.
Update 2: my jquery ajax call
$.ajax({
url: 'MyPage.aspx/myMethod',
data: JSON.stringify({ parameters: request }),
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
...
},
error: function (data) {
// data.responseJSON is undefined and data.responseText contains only HTTP status code text, like "Bad Request" when code was 400.
}
});