简体   繁体   中英

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.

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

public JsonResult GetItemByIdForEdit(int id)

The 500 code would normally indicate an error on the server, not anything with your code. Some thoughts

Talk to the server developer for more info. You can't get more info directly. Verify your arguments into the call (values). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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