1
votes

I'm using angular2-rc4 with spring rest. I'm using spring authentication.
Spring authentication expects content-type to be application/x-www-form-urlencoded; charset=UTF-8 and post data in plain string format like "j_username=user&j_password=pass".
So I wrote the following code:

let data = 'j_username=user&j_password=password'; //Spring authentication expects data in this format.
let headers = new Headers({'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'});
let options = new RequestOptions({headers: headers});
 this.http.post(url, data, options).subscribe(
.....
....
);

When i execute this code through firefox, it going through without any issue. Data is reaching to server and authentication is working fine.
But when I do from chrome, its not working. Username and password are reaching to server with empty values.
I compared the request header of both chrome and firefox with the help of firebug. I noticed one difference. content-type is going properly in firefox but not in chrome. Below is the content type of both chrome and firefox:
Firefox: Content-type: application/x-www-form-urlencoded; charset=UTF-8
Chrome : Content-type: test/plain, application/x-www-form-urlencoded; charset=UTF-8

If you see chrome is automatically appending text/plain to its header. I'm not getting the reason behind this.
Note: This code was working with angular2-rc1. Started facing issue after upgrading to rc4.
Please help
Thanks

1
There's an issue on Angular2's Github regarding this. Issue #9452, so It looks like in RC5+ they'll have this fixed. - Post Impatica

1 Answers

0
votes

I was struggling for this issue since 2 days and just after posting this question, I got solution in angular2-rc4 code. I'm posting solution here so that it can help others.
I changed my code as shown below and it started working: From: let headers = new Headers({'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}); To: let headers = new Headers(); headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

But I'm still not sure how the previous code was working in firefox.