Angular 接收对象数组asp net API
我有一个angular应用程序,它发送一个对象数组和一个.NETAPI服务器 问题是我的.NET确实收到了数据,但它无法将其正确绑定到对象 角度前端应用程序:Angular 接收对象数组asp net API,angular,api,asp.net-core,Angular,Api,Asp.net Core,我有一个angular应用程序,它发送一个对象数组和一个.NETAPI服务器 问题是我的.NET确实收到了数据,但它无法将其正确绑定到对象 角度前端应用程序: data: IAccountBookKeeping[] = []; onClickSendToAPI(){ const httpOptions = { headers: new HttpHeaders({'Content-Type': 'application/json'}) } const stringify = fastJ
data: IAccountBookKeeping[] = [];
onClickSendToAPI(){
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
}
const stringify = fastJson({
type: 'object',
properties: {
AccountingDate: {
type: 'string'
},
RegistrationNo: {
type: 'string'
},
Currency: {
type: 'string'
},
IDKT: {
type: 'string'
},
OriginalIDKT: {
type: 'string'
},
CounterAccountIDKT: {
type: 'string'
},
ProjectCode: {
type: 'string'
},
Balance: {
type: 'string'
},
Text: {
type: 'string'
}
}
})
const jsonString: string[] = [];
this.data.forEach(element => {
jsonString.push(stringify(this.data[0]));
});
this.http.post('http://localhost:5000/api/GFSAccount',
JSON.stringify(jsonString),
httpOptions).subscribe(reponse =>
console.log(reponse));
}
什么样的前端发送到API
[”
“货币”方面:“DKK\”,“IDKT\”,“IDKT\”:“34HS991016\”,“账户账户账户日期”方面,”账户账户账户日期”方面,”账户账户账户日期”方面,”账户开户日期”方面,”账户账户日期”方面,”账户账户日期”方面,”账户账户日期”方面::“2017171130\”,“注册号注册号”方面,“注册号”方面,“注册号”方面,注册号:“,,“注册号号号号:““39A1.39A1.1\,,,,,,“货币”货币”货币”方面::::::“货币”货币::::::“391.398.8.8\,,,,,,,“平衡”账户账户账户,”平衡,”平衡,”余额:“::::“1\,,,,,,“平衡:“::“1\,,,,,,,,,,,“平衡”10,”1,”1,”,”,”,”,”平衡,”,“IDKT\”:\“34HS991016\“,”原始有效期\“:”测试\“,”会计科目IDKT \“:”34HS980876\“,”项目代码\“:”078\“,”余额\“:”1\“,”文本\“:”006最长有效期2017年11月17日\“}”
ASP.NETAPI服务器接收
[HttpPost]
public IActionResult Post([FromBody] IEnumerable<AccountBookKeeping> request)
{
return Ok(request);
}
如果您想将数组作为
post
的主体发布,那么只需直接传入数组。无需通过JSON.stringify
对其进行大量处理
consturl='1〕http://localhost:5000/api/GFSAccount';
this.http.post(url、this.data、httpOptions).subscribe(response=>
控制台日志(响应);
});
另一方面,现有循环只是构建一个重复第一个元素的数组:
this.data.forEach(element => {
jsonString.push(stringify(this.data[0]));
});
但是,如果您只是将数组作为正文传入,则此步骤是不相关的。这是什么。data?您要发布的数组?是的,这是我要发送的数组:data:iaccountbookoking[]=[];我尝试了这个。ASP.NET将返回一个空数组。我还将获得一个状态代码307临时重定向。关于
public IActionResult Post(AccountBooking[]request)
?这将给出201 Ok,但返回一个空数组为什么是201?您的代码返回的Ok
应该是200。当我运行this.http.Post(`${apirl}/test`,[1,2,3])
MyC#(框架)控制器接收数据:publicIHTTpactionResult测试(int[]数据)
200。
this.data.forEach(element => {
jsonString.push(stringify(this.data[0]));
});