Wanadjanan Klinnooch Wanadjanan Klinnooch - 2 months ago 13
HTTP Question

How can I pass string like this by http.post angular2

I have a function that will be used when 'click' like this.

addTest(): void{

let headers = new Headers({ 'Content-Type': 'application/json' });
let body = '';
.map((res: Response) => res.json())
.subscribe( (res: Response) => { this.onRequestChange.emit({ isCreate:false}) });


so if i need to send string like this

[{"purchaseId":"0001","Name":"สินค้า กs","Date":"2018-06-13T00:00:00","Price":5000,"NumberQty":2,"TtlPrice":10000}]

so I taked it to

let body = '[{"purchaseId":"0001","Name":"สินค้า กs","Date":"2018-06-13T00:00:00","Price":5000,"NumberQty":2,"TtlPrice":10000}]';

but my value that was sent is null

so How can I send string like this to insert to DB (SQL Server 2014)

Answer Source

From my experience this works

addTest (url:string, data:any): Observable<any>  {

  var headers = new Headers({ 'Content-Type': 'application/json'});
  var options = new RequestOptions({ headers: headers, });
  return this.http.post(url,data,options)
                .map((res: Response): Episodes => res.json())

with data being a json object