【发布时间】:2021-12-12 12:12:28
【问题描述】:
我试图根据 2 个参数获取值,下面是我在 JSON stringify 中添加我的 2 个参数的函数:
function GetItemLocationOnHand(itemId, locationId) {
var data = JSON.stringify({
itemId: itemId,
locationId: locationId
});
$.ajax({
async: true,
type: 'GET',
dataType: 'JSON',
contentType: 'application/json; charset=utf-8',
data: data,
url: 'getItemInventory3',
success: function (data) {
$("#txtInventory3").val(parseFloat(data).toFixed(2));
},
error: function () {
alert("Error")
}
});
}
下面是我在控制器中的代码,用于根据这两个参数检索我想要的数据:
[HttpGet]
public JsonResult GetItemLocationOnHand(int itemId, int locationId)
{
var itemLocQuantity = objDB.ItemLocationDatas.Single(items => items.ItemId == itemId && items.LocationId == locationId).Quantity;
return Json(itemLocQuantity, JsonRequestBehavior.AllowGet);
}
在change 代码上通过下面调用此函数时,我似乎无法获取我的数据并且总是返回错误。如果我只有 1 个参数,则没有遇到错误。
请告知尝试传递 2 个参数时出了什么问题。
$("#LocationId").change(function () {
var itemId = $("#ItemId").val();
var locationId = $("#LocationId").val();
GetItemLocationOnHand(itemId, locationId)
});
【问题讨论】:
-
请附上相关的html
-
console.log(data)给你什么,就在$.ajax之前 -
总是返回错误 ...错误是什么?
-
` url: 'getItemInventory3' ` ?您要调用的完整网址是什么?
-
那么,您想发送这个
site1.dtempurl.com/Inventory/getItemInventory3?{"itemId":"11","locationId":"7"}而不是这个site1.dtempurl.com/Inventory/getItemInventory3?itemId=11&locationId=7或site1.dtempurl.com/Inventory/getItemInventory3?json={"itemId":"11","locationId":"7"}?
标签: c# jquery json asp.net-mvc onchange