gpt4 book ai didi

javascript - 如何将 JS 对象集合发送到 ASP.NET API 服务?

转载 作者:行者123 更新时间:2023-11-29 10:38:35 25 4
gpt4 key购买 nike

我正在尝试将 JavaScript 对象集合发送到我的 API 服务,但服务器收到空对象列表!

<script>

//Collection
var Collection = function () {
this.count = 0;
this.collection = {};

this.add = function (key, item) {
if (this.collection[key] != undefined)
return undefined;
this.collection[key] = item;
return ++this.count
}
this.remove = function (key) {
if (this.collection[key] == undefined)
return undefined;
delete this.collection[key]
return --this.count
}
this.item = function (key) {
return this.collection[key];
}
this.forEach = function (block) {
for (key in this.collection) {
if (this.collection.hasOwnProperty(key)) {
block(this.collection[key]);
}
}
}
}

// the JavaScript class for food
function foodcls(no,qunt,name, cals, carb, fat, prt,unt) {
this.no = no;
this.qunt = qunt;
this.name = name;
this.cals = cals;
this.carb = carb;
this.fat = fat;
this.prt = prt;
this.unt = unt;
}
// instantiate new obj
var fod = new foodcls(3, 5, 'SomeName', 300, 180, 100, 20, 'Gram');
var fno =333;


var timCol = new Collection();
timCol.add(fno, fod);

var urlpaths = '/api/FoodServ';
$.ajax({
url: urlpaths,
method: "POST",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(timCol),
success: function (data) {
// any thing
}
});

</script>

ASP.NET API 中的代码:

      [HttpPost]
public HttpResponseMessage Post(List<foodcls> fods) // Here fods is empty
{
int rslt=0;
string UserId = "sam@am.com";//User.Identity.Name;
List<Foods_Meal_TBL> fooditems = new List<Foods_Meal_TBL>();
if (fods.Count()>0)
{
foreach (foodcls item in fods)
{
Foods_Meal_TBL fooditem = new Foods_Meal_TBL();
fooditem.FoodNo = item.no;
fooditem.Quantity = item.qunt;
fooditem.PersonID = UserId;
fooditems.Add(fooditem);
}
}
rslt = SaveAllItems(fooditems); // rslt Meal No
return Request.CreateResponse(HttpStatusCode.OK, rslt);
}

有什么帮助吗?

最佳答案

我发现通过将集合转换为数组来解决问题,我知道这不是最好的解决方案,我希望有人能找到更好的解决方案来避免使用数组,但在有人给出更好的答案之前,我把我的解决方法如下:我添加了函数 toArr//意味着隐蔽集合到数组,如下所示:

//Collection
var Collection = function () {
this.count = 0;
this.collection = {};

this.add = function (key, item) {
if (this.collection[key] != undefined)
return undefined;
this.collection[key] = item;
return ++this.count
}
this.remove = function (key) {
if (this.collection[key] == undefined)
return undefined;
delete this.collection[key]
return --this.count
}
this.item = function (key) {
return this.collection[key];
}
this.forEach = function (block) {
for (key in this.collection) {
if (this.collection.hasOwnProperty(key)) {
block(this.collection[key]);
}
}
}
this.toArr = function (block) { //ToArray
var fodarr = [];
for (key in this.collection) {
if (this.collection.hasOwnProperty(key)) {
fodarr.push(this.collection[key]);// block(this.collection[key]);
}
}
return fodarr;
}

ASP.NET API 函数:

  [HttpPost]
public HttpResponseMessage Post(foodcls[] fods)

希望对以后的人有所帮助...

关于javascript - 如何将 JS 对象集合发送到 ASP.NET API 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32867074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com