执行Ajax时的内部服务器错误

本文关键字:服务器 错误 内部 Ajax 执行 | 更新日期: 2023-09-27 17:50:57

我有一个JS脚本:

$(document).ready(function () {
    $('.z').on('click', function (event) {
        event.preventDefault();
        $.ajax({
            url: "/DeviceUsage/Edit",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            headers: {
                'RequestVerificationToken': '@TokenHeaderValue()'
            },
            data: JSON.stringify({
                deviceusage: {
                    DeviceInstanceId: $('.a').children("#DeviceInstanceId").val(),
                    UserId: $('.a').children('#UserId').val(),
                    storageId: $('.a').children('#storageId').val()
                }
            }),
            error: function (data) {
                alert("wystąpił nieokreślony błąd " + data);
            },
            success: function (data) {
                alert(data.newurl);
                if (data.ok) {
                    $("#Modal").modal('hide');
                    window.location = data.newurl;
                }
                else {
                    $('.modal-body').html(data);
                }
            }
        })
    })
@functions{
    public string TokenHeaderValue()
    {
        string cookieToken, formToken;
        AntiForgery.GetTokens(null, out cookieToken, out formToken);
        return cookieToken + ":" + formToken;
    }
}
});

和一个方法在DeviceUsage控制器:

[HttpPost]
    [AdminAuthorization]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include="StorageId,UserId,DeviceInstanceId")] DeviceUsage deviceusage)
    {
        if (deviceusage.UserId == 6 && deviceusage.StorageId==3)
        {
            ModelState.AddModelError("", "Zarezerwowane urządzenie nie moze byc przypisane do biurka");
        }
        if(deviceusage.UserId==1 && deviceusage.StorageId==3)
        {
            ModelState.AddModelError("", "Wolne urządzenie nie może przebywać na jakimś biurku");
        }
        if((deviceusage.UserId!=1 & deviceusage.UserId!=6)&deviceusage.StorageId!=3)
        {
            ModelState.AddModelError("", "Urzązenie przypisane do kogos nie moze przebywac w magazynie");
        }
        if (ModelState.IsValid)
        {
            unitOfWork.deviceUsageRepository.Update(deviceusage);
            unitOfWork.Save();
            return Json(new { ok = true, newurl = Url.Action("Index") });
        }
        ViewBag.DeviceInstanceId = new SelectList(unitOfWork.deviceInstanceRepository.Get(), "Id", "SerialNo", deviceusage.DeviceInstanceId);
        ViewBag.StorageId = new SelectList(unitOfWork.storageRepository.Get(), "Id", "Name", deviceusage.StorageId);
        var data = unitOfWork.userRepository.Get()
        .Select(s => new
        {
            Id = s.Id,
            Credentials = s.Name + " " + s.Surname
        }
        );
        ViewBag.UserId = new SelectList(data, "Id", "Credentials", deviceusage.UserId);
        return PartialView(deviceusage);
    }

我试图在c#方法的开始放置一个断点,但它从未被击中,所以错误必须在其他地方。你能告诉我我哪里做错了吗?

执行Ajax时的内部服务器错误

我猜这两个属性中的任何一个都限制了对函数的访问。

[AdminAuthorization]
[ValidateAntiForgeryToken]

500内部服务器错误通常意味着在您调用的页面(/DeviceUsage/Edit)中存在致命错误

您可以尝试使用在页面中设置的变量手动运行页面,而不是发布它们。