angular http post发送复杂对象

时间:2017-12-26 12:16:38

标签: json angular typescript asp.net-web-api angular-httpclient

我的服务

 public submitBooking(createBooking: CreateBooking) {
    const body = this.getSaveBookingRequestBody(createBooking);
    //const json = JSON.Stringify(body) //It throws 415 error
    return this.httpClient.post(this.baseUrl + 'Save', body )
               .subscribe();
}

private getSaveBookingRequestBody(createBooking: CreateBooking): any {

    const body = {
        'Booking': {
            'Header': this.getBookingHeader(createBooking), //It works fine.
            'Items': [this.getBookingItems(createBooking)],
        },
        'TestOnly': !environment.production,
    };

    this.Logger.log(body);
    return body;
}


private getBookingItems(createBooking: CreateBooking): any {
    const bookingItem = {

        'CountryOfOrigin': createBooking.booking.countryoforigin,
        'VehicleValue': createBooking.booking.valueofvechicle,
        'SerialNumber': createBooking.booking.bookingNumber,
        'Description': createBooking.booking.description,
        'Mobility': createBooking.booking.mobility,
        'Currency': createBooking.booking.currency,
        'Weight': createBooking.booking.weight,
        'Year': createBooking.booking.year,
        'Cbm': createBooking.booking.cbm,

        //This is an array it doesn't work.
        'SubUnits':[
            createBooking.booking.relatedVehicles.forEach(element => {
                const units = {
                    'RelationType': element.relation,
                    'Weight': element.weight,
                    'Year': element.year,
                };
            })],
    };

    return bookingItem;
  

当我创建POST正文时,SubUnits在WEB API中始终为空。   如何遍历数组并创建一个对象以便作为正文发送。

我的角度模型和WEB-API对象不同

我还尝试了JSON.Stringify(unitsArray)并将其返回到SubUnits

const unitsArray = [];

createBooking.booking.relatedVehicles.forEach(element => {
        const units = {
            'RelationType': element.relation,
            'SerialNumber': element.chassisno,
            'Weight': element.weight,
            'Year': element.year,
        };
        unitsArray.push(units);
    });
SubUnits : JSON.stringify(unitsArray); // It also doesn't work with API.

版本: Angular 5 打字稿2.4.2

2 个答案:

答案 0 :(得分:1)

您是否尝试过不使用JSON.stringify?只需使用:

  SubUnits :unitsArray;

答案 1 :(得分:1)

const bookingItem = {
...
    'SubUnits': [
        createBooking.booking.relatedVehicles.forEach(element => {
            const units = {
                'RelationType': element.relation,
                'Weight': element.weight,
                'Year': element.year,
            };
        })
    ]
...
};

此循环不会填充bookingItem.SubUnits中的数组。 Array.forEach不会返回值。除此之外,从不使用变量units。你可以做的是使用Array.map创建一个新数组。

'SubUnits': [
    createBooking.booking.relatedVehicles.map(element => ({
        'RelationType': element.relation,
        'Weight': element.weight,
        'Year': element.year
    }))
]

这使得一个数组包含来自createBooking.booking.relatedVechiles的1个数组元素。我不确定这是不是你想要的,但它在你的OP中。