Web API post 数组但 frombody 始终为 null
Web API post array but frombody is always null
我有以下格式的数组,我需要 post 到 API :
console.log(addserverList);
我想将其传递给 api
的 post 方法
const options = {headers: {'Content-Type': 'application/json'}};
return this.http.post( 'http://localhost:54721/api/BulkUpload/SaveBulkUploadData',addserverList,options)
我可以post到api,但是数据传递总是显示为NULL
模型 i 的结构如下:
函数生成数组
private extractData(res: Response) {
let csvData = res['_body'] || '';
let allTextLines = csvData.split(/\r\n|\n/);
let headers = allTextLines[0].split(',');
let lines = [];
for ( let i = 0; i < allTextLines.length; i++) {
// split content based on comma
let data = allTextLines[i].split(',');
if (data.length == headers.length) {
let tarr = [];
for ( let j = 0; j < headers.length; j++) {
tarr.push(data[j]);
}
lines.push(tarr);
}
}
this.csvData = lines;
// console.log(JSON.stringify(this.csvData));
this.saveBulkUpload();
}
你的JSON是一个数组的数组。该方法需要一个与 AddServer
class.
格式匹配的对象数组
您的 JSON 应如下所示:
[
{
"HostName": "Server1",
"Type1": "type1_1",
"Type2": "type1_1",
},
{
"HostName": "Server2",
"Type1": "type1_2",
"Type2": "type2_2",
}
]
更改功能
这需要一些猜测,因为我不知道提供给函数的服务合同,但需要更改的是在循环中。
for ( let i = 0; i < allTextLines.length; i++) {
// split content based on comma
let data = allTextLines[i].split(',');
if (data.length == headers.length) {
let tobj = {};
tobj.HostName = data[0];
tobj.Type1= data[1];
tobj.Type2= data[2];
lines.push(tobj);
}
}
我有以下格式的数组,我需要 post 到 API :
console.log(addserverList);
我想将其传递给 api
的 post 方法 const options = {headers: {'Content-Type': 'application/json'}};
return this.http.post( 'http://localhost:54721/api/BulkUpload/SaveBulkUploadData',addserverList,options)
我可以post到api,但是数据传递总是显示为NULL
模型 i 的结构如下:
函数生成数组
private extractData(res: Response) {
let csvData = res['_body'] || '';
let allTextLines = csvData.split(/\r\n|\n/);
let headers = allTextLines[0].split(',');
let lines = [];
for ( let i = 0; i < allTextLines.length; i++) {
// split content based on comma
let data = allTextLines[i].split(',');
if (data.length == headers.length) {
let tarr = [];
for ( let j = 0; j < headers.length; j++) {
tarr.push(data[j]);
}
lines.push(tarr);
}
}
this.csvData = lines;
// console.log(JSON.stringify(this.csvData));
this.saveBulkUpload();
}
你的JSON是一个数组的数组。该方法需要一个与 AddServer
class.
您的 JSON 应如下所示:
[
{
"HostName": "Server1",
"Type1": "type1_1",
"Type2": "type1_1",
},
{
"HostName": "Server2",
"Type1": "type1_2",
"Type2": "type2_2",
}
]
更改功能
这需要一些猜测,因为我不知道提供给函数的服务合同,但需要更改的是在循环中。
for ( let i = 0; i < allTextLines.length; i++) {
// split content based on comma
let data = allTextLines[i].split(',');
if (data.length == headers.length) {
let tobj = {};
tobj.HostName = data[0];
tobj.Type1= data[1];
tobj.Type2= data[2];
lines.push(tobj);
}
}