Reputation: 6242
I have the following code:
var headers = new Headers();
// headers.append('Content-Type', 'application/json');
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post(
'http://192.168.1.45:3000/testrestapi',
{headers: headers}
).map((res => res.json())
.subscribe(data => {
// we've got back the raw data, now generate the core schedule data
// and save the data for later reference
this.data = data;
console.log('Friends Provider was a success!');
console.log(JSON.stringify(data));
resolve(this.data);
},
(err)=>{
console.log('Error in Friends Provider!');
},
()=>{
console.log('Friends Provider network call has ended!');
}
)
)
});
The code compiles fine without error, but I get the following errors in my ide:
I am following this docs: https://angular.io/docs/js/latest/api/http/Http-class.html . It shows how to use HTTP for GET, but not for POST. It seems I am missing the type for my headers which I am unsure what to put here as well it complains about the type for the subscribe method and again I am unsure what to put here as I am following the docs and it does not seem to have anything different?
Upvotes: 0
Views: 637
Reputation: 40647
You have imported Http module 2 times in 2nd and 3rd lines of your code, one of them is redundant.
API for http post is:
post(url: string, body: string, options?: RequestOptionsArgs) : Observable<Response>
in https://angular.io/api/http/Http
it expects a JSON.stringified body as the second parameter but you are pushing the header options which should be the third parameter.
I also suggest using
headers.set('Content-Type', 'application/json');
instead of append
.
Also you are missing the paranthesis in subscribe after data.
.subscribe(
(data) => {
console.log('Do something with data here \n',data);
}
);
Upvotes: 1
Reputation: 276189
The code compiles fine without error,
Just because you get valid JavaScript doesn't mean that it compiled without error. This is actually a TypeScript feature (see why typescript).
The errors you are seeing in the error are the compiler errrors
Upvotes: 0
Reputation: 55443
var headers = new Headers();
// headers.append('Content-Type', 'application/json');
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post(
'http://192.168.1.45:3000/testrestapi',
{headers: headers}
)
.map((res => res.json())
.subscribe(data =>
{
this.data = data;
console.log('Friends Provider was a success!');
console.log(JSON.stringify(data));
//resolve(this.data);
},
(err)=>{
console.log('Error in Friends Provider!');
},
()=>{
console.log('Friends Provider network call has ended!');
}
);
Upvotes: 0