gpt4 book ai didi

c# - 将数组从 jQuery POST 传递到 C# WebApi

转载 作者:行者123 更新时间:2023-11-30 18:22:47 25 4
gpt4 key购买 nike

我正在尝试将数组从 jQuery POST 传递到 C# WebApi 服务,但出现以下错误:

object reference not set to an instance of an object.

jQuery 代码:

 $(document).ready(function ()
{
$("#btn").click(function ()
{
var purchases = { dealid1: ["2", "3", "4"], theatid1: "5", mouvid1: "408", bookdt1: "10/11/2015", shtime1: "11:00AM", fbid1: "100" };
$.ajax(
{
type: "POST",
data: { purchases },
url: "api/purchase",
contentType: "application/json"
});
});
});
<input type="button" value="mybtn" id="btn" />

WebAPI代码:

 public object post([FromBody] purchase purchases)
{
return Request.CreateResponse(jsonvalues(purchases));
}

private object jsonvalues(purchase purchases)
{
object result = false;
for (int i = 0; i < purchases.dealid1.Length; i++)//I got error here//
{
int count= purchases.dealid1.Count();
dealid = purchases.dealid1;
string arr = dealid[i];
}
theatid = purchases.theatid1;
mouvid = purchases.mouvid1;
bookdt = purchases.bookdt1;
shtime = purchases.shtime1;
fbid = purchases.fbid1;
}
public class purchase
{
public string[] dealid1 { get; set; }
public string theatid1 { get; set; }
public string mouvid1 { get; set; }
public string bookdt1 { get; set; }
public string shtime1 { get; set; }
public string fbid1 { get; set; }

}

我能够获取其他值,如 theatid1mouvid1 等。但是 dealid1 不可访问。知道我做错了什么吗?

最佳答案

$(document).ready(function ()
{
$("#btn").click(function ()
{
var purchases = { dealid1: ["2", "3", "4"], theatid1: "5", mouvid1: "408", bookdt1: "10/11/2015", shtime1: "11:00AM", fbid1: "100" };
$.ajax(
{
type: "POST",
data: JSON.stringify( purchases ),
url: "api/purchase",
contentType: "application/json"
});
});
});

关于c# - 将数组从 jQuery POST 传递到 C# WebApi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33537127/

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