【发布时间】:2017-09-07 17:41:09
【问题描述】:
在我的 ASP.Net Web Api 2 应用程序中,当单击提交按钮时,我使用 AJAX 发布将注册表单数据发送到服务器。
function click_form() {
var formData = JSON.stringify({
Name: $('#regInputName').val(),
SecondName: $('#regInputLastName').val(),
Email: $('#regInputEmail').val(),
Username: $('#regInputUsername').val(),
Password: $('#regInputPassword').val(),
Contact: $('#regInputContact').val(),
});
$.ajax({
url: '/api/Account/registration',
method: 'POST',
data: formData,
contentType: 'application/json;charset=utf-8',
dataType: 'json',
cache: false,
success: function () {
alert("Success");
},
error: function () {
alter('Error');
}
});
}
这是我的 Web Api 控制器,如果用户名不存在,我将返回 HttpStatusCode.OK,如果用户名已经存在,则返回 HttpStatusCode.NotFound。
[HttpPost]
[Route("api/Account/registration")]
public HttpResponseMessage registration([FromBody] UserRegModel user)
{
if (!usernames[0].Contains(user.Username))
{
User u = new User(){/*....*/}
/*... updating database ... */
HttpContext.Current.Session["user"] = u;
return new HttpResponseMessage(HttpStatusCode.OK);
}
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
我的控制器每次都返回 OK 或 NotFound 但 Ajax 事件成功和错误永远不会被触发。
有人知道问题出在哪里吗?
【问题讨论】:
-
您在错误处理程序中有错字 -
alert不是alter。 -
你是对的,但这并不能解决我的问题。
-
您在控制台中看到了什么?有什么错误吗?通话被屏蔽了吗?
标签: javascript jquery asp.net ajax asp.net-web-api2