简体   繁体   English

执行Ajax时出现500内部服务器错误

[英]500 Internal server error when executing Ajax

I have a JS script: 我有一个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;
    }
}
});

And a method In DeviceUsage controller: 和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);
    }

I was trying to put a breakpoint at the start of the C# method but it was never hit so Error must be somewhere else. 我试图在C#方法的开头设置一个断点,但它从未被命中,因此Error必须在其他地方。 Can you tell me what I'm doing wrong? 你能告诉我我做错了什么吗?

I guess any of these two attribute restricting to access the function. 我猜这两个属性中的任何一个都限制访问该函数。

[AdminAuthorization]
[ValidateAntiForgeryToken]

remove it and try once. 删除它并尝试一次。

500 Internal server error usually means there was a fatal error in the page you called (/DeviceUsage/Edit) 500内部服务器错误通常表示您调用的页面中存在致命错误(/ DeviceUsage / Edit)

You can try manually running the page with the variables set in the page instead of posting them. 您可以尝试使用页面中设置的变量手动运行页面,而不是发布它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ajax调用WebMethod时出现500 Internal Server Error - Getting 500 Internal Server Error when calling a WebMethod using ajax 500内部服务器错误与Ajax请求 - 500 Internal Server Error With Ajax Request Ajax调用返回500内部服务器错误 - Ajax call returning 500 internal server error 在C#中调用Web服务器方法时,ajax中的500 Internal Server Error - 500 Internal Server Error in ajax when calling web server method in C# Ajax将数据发布到服务器端-错误500内部服务器错误 - Ajax post data to server side - Error 500 internal server error 出现错误:使用 AJAX POST 到 C# Webmethod 时出现 500 内部服务器错误 - Getting Error: 500 Internal server error when using AJAX POST to C# Webmethod 从ajax调用WebApi时如何调试500内部服务器错误? - How can I debug a 500 Internal Server Error when calling a WebApi from ajax? 将base64图像提交到jQuery ajax和Web方法时,生产中出现500 Internal Server Error - 500 Internal Server Error in production when submitting base64 image to jquery ajax and web method AJAX 在调用 ASP.NET MVC 控制器时收到 500 内部服务器错误 - AJAX receives 500 Internal server error when calling ASP.NET MVC Controller 当我尝试在jQuery ajax中发布json时出现内部服务器错误500 - I have internal server error 500 when I tried to post json in Jquery ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM