繁体   English   中英

Ajax 调用资源加载失败:服务器响应状态为 500(内部服务器错误)

[英]Ajax call with a failed to load resource: the server response with a status 500 (internal server error)

我创建了一个 function 来执行 ajax 调用,但调用它时出现此错误。

Failed to load resource: the server responded with a status of 500 (Internal Server Error) 

这是代码:

 $('#Item').change(function () {
        debugger
        $('#Price').empty();
        $('#Description').empty();
        $('#Quantity').val("");
        $('#Amount').val("");
        $.ajax({
            type: "GET",
            url: "/Payments/GetItemByIdForEdit/",
            datatype: "Json",
            data: { id: $('#Item').val() },
            success: function (data) {
                debugger

                $("#Price").val(data.ItemUnitPrice);
                $("#Description").val(data.itemDescription);
                //$("#productId").text(value.Id);



            }
        });
    });

这是动作代码:

public JsonResult GetItemByIdForEdit(string id)
    {
        if (id != "")
        {
            int productId = Convert.ToInt32(id);
            var product = _unitOfWork.Items.GetItemSingleOrDefault(productId);
            return Json(product, JsonRequestBehavior.AllowGet);
        }
        else
        {
            var product = new Item();
            //product.Description = "";
            //product.UnitPrice = 0.0;
            return Json(product, JsonRequestBehavior.AllowGet);

        }
    }

我看不出问题出在哪里?

你有什么解决办法?

谢谢你

这是调试器模式的结果:

在此处输入图像描述

您正在将空值传递给控制器​​。 您必须首先对您的参数进行字符串化。 例子:

    var postData = JSON.stringify({
        'id': $('#Item').val()
    });

    $.ajax({
        type: "POST",
        url: "/Payments/GetItemByIdForEdit/",
        datatype: "application/json",
        data: postData,
        success: function (data) {
            debugger

            $("#Price").val(data.ItemUnitPrice);
            $("#Description").val(data.itemDescription);
            //$("#productId").text(value.Id);



        }
    });

在您的方法中,您可以直接使用 int

public JsonResult GetItemByIdForEdit(int id)

500 代码通常表示服务器错误,而不是您的代码。 一些想法

与服务器开发人员联系以获取更多信息。 您无法直接获取更多信息。 验证您的 arguments 是否进入通话(值)。 查找您可能认为可能导致服务器进程出现问题的任何内容。 这个过程不应该死掉并且应该返回一个更好的代码,但是错误也会在那里发生。 可能是间歇性的,例如服务器数据库出现故障。 可能值得在其他时间尝试。

同一问题的解决方案之一是重复发布 url。更改发布 URL 并重试。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM