简体   繁体   English

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

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

I created a function that do an ajax call but I have this error when I call it.我创建了一个 function 来执行 ajax 调用,但调用它时出现此错误。

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

This is the code:这是代码:

 $('#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);



            }
        });
    });

this is the action code:这是动作代码:

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

        }
    }

I don't see where is the problem?我看不出问题出在哪里?

Do you have any solutions?你有什么解决办法?

thank you谢谢你

this is the debugger mode result:这是调试器模式的结果:

在此处输入图像描述

You are passing null value to controller.您正在将空值传递给控制器​​。 You have to stringify your paremater first.您必须首先对您的参数进行字符串化。 Example:例子:

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



        }
    });

In your method you can use int directly在您的方法中,您可以直接使用 int

public JsonResult GetItemByIdForEdit(int id)

The 500 code would normally indicate an error on the server, not anything with your code. 500 代码通常表示服务器错误,而不是您的代码。 Some thoughts一些想法

Talk to the server developer for more info.与服务器开发人员联系以获取更多信息。 You can't get more info directly.您无法直接获取更多信息。 Verify your arguments into the call (values).验证您的 arguments 是否进入通话(值)。 Look for anything you might think could cause a problem for the server process.查找您可能认为可能导致服务器进程出现问题的任何内容。 The process should not die and should return you a better code, but bugs happen there also.这个过程不应该死掉并且应该返回一个更好的代码,但是错误也会在那里发生。 Could be intermittent, like if the server database goes down.可能是间歇性的,例如服务器数据库出现故障。 May be worth trying at another time.可能值得在其他时间尝试。

One of same problem's solition is dublicate post url. Change post URL and try again.同一问题的解决方案之一是重复发布 url。更改发布 URL 并重试。

暂无
暂无

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

相关问题 加载资源失败:服务器响应状态为 500(内部服务器错误) - failed to load resource: the server response with a status 500 (internal server error) jQuery ajax调用将无法访问控制器:无法加载资源:服务器以状态500(内部服务器错误)响应 - Jquery ajax call wont access the controller: Failed to load resource: the server responded with a status of 500 (Internal Server Error) 加载资源失败:服务器响应状态为500(内部服务器错误 - Failed to load resource: the server responded with a status of 500 (Internal Server Error Ajax 调用“加载资源失败:服务器响应状态为 500” - Ajax call with a 'failed to load resource : the server responded with a status of 500' 无法加载资源:服务器响应状态为 500(内部服务器错误)Azure MVC - Failed to load resource: the server responded with a status of 500 (Internal Server Error) Azure MVC 加载资源失败:服务器响应状态为500(内部服务器错误)asp.net - Failed to load resource: the server responded with a status of 500 (Internal Server Error) asp.net 无法加载资源:服务器使用Ajax响应状态为500 - Failed to load resource: the server responded with a status of 500 using Ajax 加载资源失败:服务器响应状态为 500(内部服务器错误),同时从控制器返回 base64 格式的图像 - Failed to load resource: the server responded with a status of 500 (Internal Server Error) while returning image in base64 format from controller Ajax调用返回500内部服务器错误 - Ajax call returning 500 internal server error 从 ajax 调用 WCF 并收到此错误,加载失败:服务器响应状态为 500 (System.ServiceModel.ServiceActivationException) - calling WCF from ajax and getting this error,Failed to load:server responded with a status of 500 (System.ServiceModel.ServiceActivationException)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM