In angular 4, How can I post just JSON as data?
Currently, my post code is:
let options = {
headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded)
};
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('key', 'value');
let bodyString = urlSearchParams.toString();
return this.http.post(api, bodyString, options);
As you can see I am using application/x-www-form-urlencoded
and URLSearchParams
before post
. (which is working)
I think there will be a way to use just JSON
. (e.g. this.http.post(api, MY_JSON)
)
I have tried new HttpHeaders().set('Content-Type', 'application/json')
but no luck.
The below is what I want :
let options = {
headers: new HttpHeaders().set('Content-Type', 'application/json)
};
return this.http.post(api, MY_JSON, options);
If I post like the above, everything is correctly posted but no reaction from the server.
Should I check back-end?