I'm trying to call API using Angular 2, but it sends CORS error. However, If I try to call the same API from http://www.webtoolkitonline.com/javascript-tester.html (a Javascript Tester) or http://hurl.it, then it executes it correctly.
I've tried adding headers from Client side also.
My Angular Code is as below
private _apiCountryUrl = 'http://52.179.4.123/smvs/index.jsp?json={"COUNTRY":[{"ACTION":"SELECT"}]}';
getTotalCountry() : Observable<ICountry[]> {
let headersNew = new HttpHeaders();
headersNew.append('Access-Control-Allow-Headers', 'Content-Type');
headersNew.append('Access-Control-Allow-Methods', 'GET');
headersNew.append('Access-Control-Allow-Origin', '*');
return this._http.get<ICountry[]>(this._apiCountryUrl, {headers: headersNew})
.do(data => console.log('Country' + JSON.stringify(data)))
.catch(this.handleError);
}
When API is called, I'm getting output as below.
If I write code as below
$(document).ready(function() {
$.ajax({
type: 'GET',
url: 'http://52.179.4.123/smvs/index.jsp?json=%7B%22COUNTRY%22%3A%5B%7B%22ACTION%22%3A%22SELECT%22%7D%5D%7D',
success: function(res) {
alert(res);
},
error: function(error) {
alert("error!" + JSON.stringify(error));
}
});
});
The above code works correct with http://www.webtoolkitonline.com/javascript-tester.html the but same API is not called from Angular2. If it's server side error then it should not even called when jQUery request is done.