C# 加载资源失败的Ajax调用:状态为500的服务器响应(内部服务器错误)
我创建了一个执行ajax调用的函数,但在调用它时出现了这个错误C# 加载资源失败的Ajax调用:状态为500的服务器响应(内部服务器错误),c#,ajax,asp.net-mvc,C#,Ajax,Asp.net Mvc,我创建了一个执行ajax调用的函数,但在调用它时出现了这个错误 Failed to load resource: the server responded with a status of 500 (Internal Server Error) 代码如下: $('#Item').change(function () { debugger $('#Price').empty(); $('#Description').empty();
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)
使用浏览器转到该URL时会发生什么情况?转到此操作
public JsonResult GetItemByIdForEdit(字符串id)
然后在启动此函数后,成功函数不会启动success:function(data){debugger$(“#Price”).val(data.ItemUnitPrice)$(“#Description”).val(data.itemsdescription);//$(“#productId”).text(value.Id)}
并给出了错误成功函数的主要问题在调用GetItemByIdForEdit
后没有触发,在这个方法中仍然是相同的问题public JsonResult GetItemByIdForEdit(int-Id)
尝试解决方案后,id
为空,请确保您的项具有值?例如,通过console记录它。log($('#Item').val())检查我的答案您有错误的jquery代码。Postdata不是它的参数变量。