如何从Jquery ajax调用HttpPost类型的控制器方法

本文关键字:类型 控制器 方法 HttpPost 调用 Jquery ajax | 更新日期: 2023-09-27 17:54:15

我正试图从我的jquery脚本调用称为UpudateFingerprintStatus的MVC控制器函数。这是PUT呼叫,因为我正在更新所需对象的状态。当我试图调用这个方法时,我得到了一个404 error

这是我的JS代码:

    function updateStatus(statusId, fingerprintId, isDeleted, userId) {
    var confirm = window.confirm("Are you sure you wish to change the Fingerprint Status?");
    if (confirm) {
        $.ajax({
            type: "POST",
            url: "/Tools/FingerprintTool/UpdateFingerprintStatus",
            dataType: "json",
            processData: false,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({
                userId: userId,
                statusId: parseInt(statusId),
                fingerprintId: fingerprintId,
                isDeleted: isDeleted
            }),
            sucess: function(resp) {
                alert("success");
            },
            error: function(resp) {
                alert("Failure" + resp.description);
            }
        });
    }
}

这是我的控制器方法:

 [HttpPut]
        public JsonResult UpdateFingerprintStatus(int userId, int statusId, int fingerprintId, int isDeleted)
        {
            var response = _driver.UpdateFingerprintGrantById(userId, fingerprintId, isDeleted, statusId);
            return Json(response.Note);
        }

任何帮助都将非常感激!

如何从Jquery ajax调用HttpPost类型的控制器方法

当我们发送post, put请求时,它会在body中发送复杂的数据类型,因此要绑定这些复杂的数据,您需要创建一个类,其中所有属性都应该与您从前端发送的名称相同。

public class FingerprintStatus{
        public string UserId { get; set; }
        public int StatusId { get; set; }
        public int FingerprintId { get; set; }
        public bool IsDeleted { get; set; }
}
    [HttpPut]
            public JsonResult UpdateFingerprintStatus(FingerprintStatus model)
            {
                var response = _driver.UpdateFingerprintGrantById(model.UserId, model.FingerprintId, model.IsDeleted, model.StatusId);
                return Json(response.Note);
            }

应该可以:

function updateStatus(statusId, fingerprintId, isDeleted, userId) {
    var confirm = window.confirm("Are you sure you wish to change the Fingerprint Status?");
    if (confirm) {
        var domain = window.location.protocol + "//" + window.location.host;
        var url = domain + "/Tools/FingerprintTool/UpdateFingerprintStatus";
        var dataContract = {
            userId: userId,
            statusId: parseInt(statusId),
            fingerprintId: fingerprintId,
            isDeleted: isDeleted
        };
        $.ajax({
            type: "PUT",
            url: url,
            dataType: "json",
            data: dataContract,
            sucess: function(resp) {
                alert("success");
            },
            error: function(resp) {
                alert("Failure" + resp.description);
            }
        });
    }
}
[HttpPut]
public JsonResult UpdateFingerprintStatus(int userId, int statusId, int fingerprintId, int isDeleted)
{
    var response = _driver.UpdateFingerprintGrantById(userId, fingerprintId, isDeleted, statusId);
    return Json(response.Note);
}