我正在创建webapi
(MVC
)来验证用户,我从jquery调用“ValidateUser
”(没有HttpGet),但是我收到错误“方法不允许”。为什么呢?
是否可以在同一个UserController中拥有多个get方法? (productdetails
的另一种方法)。如何从jquery
拨打电话?
public class UserController : ApiController
{
private VAMDESchedulerDBEntities db = new VAMDESchedulerDBEntities();
public IHttpActionResult ValidateUser(string username, string userpassword)
{
var userlist = DBActivity.GetUserList();
var userdetails = userlist.FirstOrDefault(x => x.UserName == username);
if (userdetails == null)
{
return NotFound();
}
if (userdetails.UserPassword == userpassword)
return Ok(userdetails);
else
return NotFound();
}
的index.html
<script>
var uri2 = 'api/user/ValidateUser?username=xxx&userpassword=xxx';
$(document).ready(function () {
alert('document ready ' + uri2);
// Send an AJAX request
$.getJSON(uri2)
.done(function (data) {
alert(data.UserName + '- PWD : ' + data.UserPassword);
})
.fail(function (jqXHR, textStatus, err) {
alert('users api fail: ' + err);
});
});
</script>