当我发送我的 Ajax 请求时,我可以看到返回的状态代码是 200,这意味着一切正常。但是,它总是执行错误函数而不是成功函数。
网络 API
public HttpResponseMessage ChangeModus(Artikel Artikel)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
JavaScript
$.ajax({
type: 'POST',
dataType: 'json',
contentType: 'application/json',
url: "/Api/Artikel/ChangeModus",
data: JSON.stringify(Artikel),
success: OnSaveSuccess,
error: OnSaveError
});
function OnSaveSuccess(response) {
alert("Success")
}
function OnSaveError(response) {
alert("Ein Fehler ist aufgetreten");
}
是我做错了什么还是为什么总是执行Error函数?
最佳答案
发生错误是因为尽管您有一个 200 (OK) 响应,但该响应不包含响应正文。您应该在响应中发送内容,或者改用 HttpStatusCode.NoContent
(204)。
关于javascript - Ajax 总是在成功时使用 Asp.Net HttpResponseMessage 执行错误函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51716421/