当我发出请求时,它抛出了一个错误:
{“ message”:“请求无效。”,“ modelState”:{“ model.packages”:[“转换值\” [{19:1},{1053:1},{1056:时出错: 0},{1059:0},{1060:0},{1061:0},{1062:0},{1063:0},{1064:0}] \“键入“ System.Collections.Generic。 Dictionary`2 [System.Int32,System.Int32]'。
所以我在后端 字典包。
该对为{id,数量},看起来应该像这样:
packages= [{23, 1},{19,5}, {33,1}].
我已经在Angular项目界面中创建了
export interface RequestPackage {
[key: string]: number;
}
和一个班级
export class CustomerBooking {
packages: RequestPackage [];
...
;
constructor(packages: RequestPackage [],
...
}
}
在我的服务中,我有:
public getSelectedBoxes(){
var selectedPackages = this.servicePackagesService.currentServicePackagesGroup.packages;
var stringifiedData;
var filteredPackages = [];
for (var i = 0; i < selectedPackages.length; i++) {
var packagesModel = {
id: null,
quantity: null,
};
packagesModel.quantity = selectedPackages[i].count;
packagesModel.id= selectedPackages[i].servicePackage.id;
filteredPackages.push(packagesModel);
stringifiedData = JSON.stringify(filteredPackages);
}
var packages: any[] = filteredBoxItems.map(i => ({ [parseInt(i.id, 10)]: i.quantity }))
var stringifyPack: any;
stringifyPack= JSON.stringify(packages);
console.log(stringifyPack);
/* in console:
[{"19":1},{"1053":1},{"1056":0},{"1059":0},{"1060":0},{"1061":0},{"1062":0},{"1063":0},{"1064":0}]
*/
var stringifyPack123= stringifyPack.toString().replace(/"/g, "");
console.log(stringifyPack123);
/* in console:
[{19:1},{1053:1},{1056:0},{1059:0},{1060:0},{1061:0},{1062:0},{1063:0},{1064:0}]
*/
return stringifyPack123;
// I have tried returning stringifyPack too, and still same error
}
我正在提出要求:
public bookNow(): Observable<BookingResult> {
var booking: CustomerBooking = new CustomerBooking(this.getSelectedPackages(),
...
);
var url = this.config.getApiUrl(this.config.requestPackages);
return this.http.post<BookingResult>(url, booking);
}
当我发出请求时,我的JSON当前看起来像这样(Fiddler):
JSON
packages=[{19:1},{1053:1},{1056:0},{1059:0},{1060:0},{1061:0},{1062:0},{1063:0},{1064:0}]
...
...
...
这给我抛出了错误:
{“ message”:“请求无效。”,“ modelState”:{“ model.packages”:[“转换值\” [{19:1},{1053:1},{1056:时出错: 0},{1059:0},{1060:0},{1061:0},{1062:0},{1063:0},{1064:0}] \“键入“ System.Collections.Generic。 Dictionary`2 [System.Int32,System.Int32]'。
我将不胜感激。我知道这段代码现在是一团糟,但是我尝试了很多版本,每次遇到相同的错误。