When I called the http post method it returns "data does not pass correctly".I passed data through body and I also tried by passing using json.stringify() I also tried by using content type as following also. But it didn't work.
const headers = new Headers({ 'Content-Type': 'application/json' });
const options = new RequestOptions({ headers: headers });
this.http_.post('http://localhost:12412/api/employee', remark, options)
.subscribe((data) => {console.log(data)})};
I attached the post call method in component.ts file below:
import { Component, OnInit } from '@angular/core';
import 'rxjs/add/operator/map';
import { Injectable } from '@angular/core';
import { Response, Headers, RequestOptions,Http} from '@angular/http';
import { HttpClient} from '@angular/common/http';
import { Time } from '@angular/common/src/i18n/locale_data_api';
import { NgbDateStruct } from '@ng-bootstrap/ng-bootstrap/datepicker/ngb-date-struct';
import { NgbCalendar } from '@ng-bootstrap/ng-bootstrap/datepicker/ngb-calendar';
import {Ng2OrderModule} from 'ng2-order-pipe';
import {Observable} from "rxjs/Rx";
import { PostChangesService } from './PostChanges.service';
import { Body } from '@angular/http/src/body';
@Component({
selector: 'app-table',
templateUrl: './table.component.html',
styleUrls: ['./table.component.css']
})
export class TableComponent implements OnInit {
constructor(private http: HttpClient, private http_:Http){
}
EditValue(event,remark,log_id) {
console.log(remark+" "+log_id );
// var headers = new Headers();
// headers.append('Content-Type', 'application/x-www-form-urlencoded');
// var body="remark="+remark;
// //let body = JSON.stringify(remark);
// this.http_.post('http://localhost:12412/api/employee?',{body:remark},{headers:headers})
// .subscribe((data) => {console.log(data)}
// );
const headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
const options = new RequestOptions({ headers: headers });
//const params = new URLSearchParams();
// let body = JSON.stringify(remark);
this.http_.post('http://localhost:12412/api/employee?',remark,options)
.subscribe((data) => {console.log(data)}
)};
}
}
And the web api post method is as follows:
[HttpPost]
public string Post([FromBody]string remark)
{
if (remark != null)
{
return remark + " _ " ;
}
else
return "data doesn't pass correctly";
}
Import Headers from @angular/http
You can try to change your code to:
No change Web API. It should be worked.
Try passing your post data as object like this:
Web api post method