0

I have been going trough this problem since yesterday, and while I have been searching through StackOverflow and other forums I couldn't find an answer to that problem.

I am looking to call a JSON array from our company's API, I was trying this code but it didn't work.

constructor(public http: Http) {
  console.log('Data Service connected...');
}

getPosts() {
  const url = 'example.com';
  const header = new Headers();
  header.set('user', 'foo');
  header.append('password', 'bar');
  header.append('Content-Type', 'application/json');
  const options = new RequestOptions({headers: header});
  return this.http.get(url, options).map(res => res.json());
}

However when I try to compile I get an error 401 (Unauthorized), is there any way to input the username and the password to get the response from the API?

EDIT

Here are the request headers:

Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,ar;q=0.6
Access-Control-Request-Headers:content-type,password,user
Access-Control-Request-Method:GET
Connection:keep-alive
Host:ess.gtslb.com
Origin:http://evil.com/
Referer:http://localhost:4200/
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
eBourgess
  • 304
  • 4
  • 15

1 Answers1

0

Would be great to see the response header, have you tried to use jsonp? My guess you query a different server, and in this case you can't use a simple ajax call.

Denis Baciu
  • 149
  • 8
  • I am calling the api from the company's server to my local machine here I will update the question to add the request headers – eBourgess Jul 20 '17 at 08:45