Ten kod używa stosu Microsoft Web Api Http i jQuery.Jak uzyskać niestandardowy komunikat o błędzie z Web Api do jQuery.ajax?
Jak mogę dostać niestandardowy komunikat o błędzie, stworzony przez httpError parametr do CreateErrorResponse(), wyświetlany przez jQuery deferred.fail()?
Przykładem tworzenia odpowiedzi błędu dla celów testowych w ApiController:
public HttpResponseMessage Post(Region region)
{
var error = new HttpError("Failure to lunch.");
return this.Request.CreateErrorResponse(
HttpStatusCode.InternalServerError,
error);
}
Oto cut-down klient, który próbuje znaleźć komunikat o błędzie wyświetlany, „Miłość na zamówienie”..
$.ajax({
type: 'POST',
url: 'api/region',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(region)
})
.fail(function (jqXhr, textStatus, errorThrown) {
alert(textStatus + ": " + errorThrown + ": " + jqXhr.responseText);
});
Co będzie wyświetlany jest:
"error: Internal Server Error: {full stack here}"
Czego chcę zamiast jest:
"Failure to lunch."
Czy masz na myśli "fa ilure wystartuje "nie lunch –
@KevinHolditch Byłem głodny! – Boggin
więc jestem teraz przeczytałem twoje pytanie :) –