如何从Jquery ajax调用

时间:2016-07-04 01:01:15

标签: javascript c# jquery ajax asp.net-mvc

我试图从我的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);
            }
        });
    }
}

这是我的Controller方法:

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

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

当我们发送帖子时,把请求发送到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);
            }

答案 1 :(得分:0)

这应该有效:

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);
}