Http.post not sending data

PHP method POST accept this format ‘Content-Type’ : ‘application/x-www-form-urlencoded’
email=example@gmail.com&password=abcd
solution:
your data like this:

let body = JSON.stringify({ email : “example@gmail.com”, password : “abcd” });

this function convert your output like this : email=example@gmail.com&password=abcd
let postdata = this.formData(body);

console.log("Result : " + postdata );

Result : email=example@gmail.com&password=abcd

you can use this function with your form
let postdata = formData(this.loginForm.value);

console.log("Result : " + postdata );

Result : email=example@gmail.com&password=abcd

//function formData convert data to ‘Content-Type’ : ‘application/x-www-form-urlencoded’

formData(myFormData){
  return Object.keys(myFormData).map(function(key){
  return encodeURIComponent(key) + '=' + encodeURIComponent(myFormData[key]);
}).join('&');

}

please change your header like this

let headers = new Headers({
// ‘Content-Type’: ‘application/json’ // This is your old content type

            'Content-Type' : 'application/x-www-form-urlencoded' // change to like this 
	});
5 Likes