Reputation: 1304
I am trying to send two values from client to server side using HTTP post in MEAN. I can add one but when I try to add two values I am getting an error.
Here is my code:
var emailid = 'emailid=' + ctinvite.emp_email;
var phone= 'phone=' + ctinvite.emp_phone;
headers.append('Content-Type', 'application/X-www-form-urlencoded');
this.http.post('http://localhost:3000/sendmail',emailid, {headers: headers})
I added emailid and it's working perfectly. Now I want to add phone along with emailid in HTTP Post.
Upvotes: 1
Views: 2421
Reputation: 11696
If you will to create an url with params like this:
http://url.com/user?emailid=12&phone=2234234234
than you should use HttpParams
(or URLSearchParams
for old (<4.3) Angular version). Here you can find a little bit more.
Here is an example based on your question data:
Angular v4.3+ with new HttpClientModule:
let httpParams = new HttpParams();
httpParams = httpParams.set('emailid', ctinvite.emp_email.toString());
httpParams = httpParams.set('phone', ctinvite.emp_phone.toString());
headers.append('Content-Type', 'application/X-www-form-urlencoded');
this.http.post('http://localhost:3000/sendmail', null, {headers: headers, params: httpParams});
Angular v4.3 and older with old HttpModule:
let params = new URLSearchParams();
params.set('emailid', ctinvite.emp_email.toString());
params.set('phone', ctinvite.emp_phone.toString());
headers.append('Content-Type', 'application/X-www-form-urlencoded');
this.http.post('http://localhost:3000/sendmail', null, {search: params, headers: headers});
If you will to send you data as FormData
(that I see on your headers settings) so here is an example for that:
const formData = new FormData();
formData.append('emailid', ctinvite.emp_email.toString());
formData.append('phone', ctinvite.emp_phone.toString());
headers.append('Content-Type', 'application/X-www-form-urlencoded');
this.http.post('http://localhost:3000/sendmail',formData, {headers: headers});
If you will to send you data as JSON object, so you should make it like this:
let jsonData = {
emailid: ctinvite.emp_email,
phone: ctinvite.emp_phone
}
headers.append('Content-Type', 'application/json');
this.http.post('http://localhost:3000/sendmail', JSON.stringify(jsonData), {headers: headers});
Upvotes: 1
Reputation: 3720
Use following code :
var emailid = 'emailid=' + ctinvite.emp_email;
var phone= 'phone=' + ctinvite.emp_phone;
headers.append('Content-Type', 'application/X-www-form-urlencoded');
this.http.post(`http://localhost:3000/sendmail=${emailid}&phone=${phone}`, {headers: headers})
Hope it will help
Upvotes: 0
Reputation: 41571
You should be using a complex object as below
var emailid = 'emailid=' + ctinvite.emp_email;
var phone= 'phone=' + ctinvite.emp_phone;\
var email_phone ={
emailid : emailid,
phone :phone
}
headers.append('Content-Type', 'application/X-www-form-urlencoded');
this.http.post('http://localhost:3000/sendmail',email_phone, {headers: headers})
Upvotes: 2