angular http post 发送复杂的对象 [英] angular http post send complex objects

查看:33
本文介绍了angular http post 发送复杂的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务

 public submitBooking(createBooking: CreateBooking) {const body = this.getSaveBookingRequestBody(createBooking);//const json = JSON.Stringify(body)//抛出415错误返回 this.httpClient.post(this.baseUrl + 'Save', body ).订阅();}私人 getSaveBookingRequestBody(createBooking: CreateBooking): 任何 {常量体 = {'预订':{'Header': this.getBookingHeader(createBooking),//工作正常.'物品':[this.getBookingItems(createBooking)],},'TestOnly': !environment.production,};this.Logger.log(body);返回身体;}私人 getBookingItems(createBooking: CreateBooking): 任何 {const 预订项 = {'CountryOfOrigin':createBooking.booking.countryoforigin,'VehicleValue':createBooking.booking.valueofvechicle,'SerialNumber': createBooking.booking.bookingNumber,'描述':createBooking.booking.description,移动性":createBooking.booking.mobility,货币":createBooking.booking.currency,'重量':createBooking.booking.weight,'年':createBooking.booking.year,'Cbm':createBooking.booking.cbm,//这是一个不起作用的数组.'子单元':[createBooking.booking.relatedVehicles.forEach(element => {常量单位 = {'RelationType': element.relation,'重量':element.weight,'年': element.year,};})],};返回预定项目;

<块引用>

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

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

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

const unitsArray = [];createBooking.booking.relatedVehicles.forEach(element => {常量单位 = {'RelationType': element.relation,'序列号':element.chassisno,'重量':element.weight,'年': element.year,};unitArray.push(units);});子单元:JSON.stringify(unitsArray);//它也不适用于 API.

版本:角 5打字稿 2.4.2

解决方案

const bookingItem = {...'子单元':[createBooking.booking.relatedVehicles.forEach(element => {常量单位 = {'RelationType': element.relation,'重量':element.weight,'年': element.year,};})]...};

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

'子单元':[createBooking.booking.relatedVehicles.map(element => ({'RelationType': element.relation,'重量':element.weight,'年': element.year}))]

这将创建一个包含来自 createBooking.booking.relatedVechiles 的 1 个数组元素的数组.我不确定这是否是您想要的,但它在您的 OP 中.

My Service

 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;

When i create POST body the SubUnits always empty in WEB API. How to loop through array and create an object for sending as body.

My angular model and WEB-API object are different

I have also tried the JSON.Stringify(unitsArray) and returning it to 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.

Version : Angular 5 Typescript 2.4.2

解决方案

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

This loop doesnt fill the array in bookingItem.SubUnits. Array.forEach does not return a value. Besides that, the variable units is never used. What you can do instead is create a new array using Array.map.

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

This makes an array with 1 array element from createBooking.booking.relatedVechiles. I am not sure if that's what you are going for, but it is in your OP.

这篇关于angular http post 发送复杂的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆