1
votes

I have a problem post complex object with rxjs observable ajax.

updateCashDesck():Observable<SomeModel>{
    return Observable.ajax({
        body: {id:5,Stocks:[{WarehouseId:5,WarehouseName:"1235"}]},
        url:this._apiServerUrl,
        method:"POST"
    }).map(r=>{
        return new SomeModel(r.response);
    }).catch((e,r)=>{
        console.log(e);
        return Observable.throw(e);
    });
}

The problem is that. When ajax send request it set content-type as:Content-Type:application/x-www-form-urlencoded; charset=UTF-8 and request data post as:

Id:5
Stocks:[object Object]

So my MVC controller does not see Stocks (Stocks: count=0).

How can i change Content-Type in Observable.ajax to application/json?

1

1 Answers

1
votes

If you look at the implementation, you'll see that the serializeBody method also supports application/json.

However, application/x-www-form-urlencoded is the default, if Content-Type is not specified.

So you need to specify application/json explicitly:

Observable.ajax({
    body: { id: 5, Stocks: [{ WarehouseId: 5, WarehouseName: "1235" }] },
    headers: { "Content-Type": "application/json" },
    url:this._apiServerUrl,
    method:"POST"
})