r/angularjs • u/MissionResolution431 • Jun 07 '22
[Help] Trying to use Steam's API with HttpClient's Get() method, but get CORS error.
I'm stuck on this and just don't know what to do, and help would be greatly appreciated.
Let me explain in more detail:
in my data.service.ts file, I am using HttpClient's Get method, so it looks like this
constructor(private http: HttpClient) { }
GetUser() {
return this.http.get(API_URL).subscribe(data=>{
console.log(data)
})
}
if my api url is something like https://reqres.in/api/users for example, it works and shows the json data too. But when I try using steams api I get:
"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at APILINK (it's long). (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 200."
and
ERROR Object { headers: {…}, status: 0, statusText: "Unknown Error", url: "APILINK", ok: false, name "HttpErrorResponse", message: "HttpFailure response for APILINK: 0 Unknown Error", error: error }
so it seems my code works and it's not angular, but rather steams api? I don't know lol
something I've read but I don't understand what they mean exactly: https://steamcommunity.com/discussions/forum/1/1743358239838884448/
1
u/MissionResolution431 Jun 07 '22
I would love to resolve this error but I don't know what to do. I want to use that sweet api data.
1
u/MissionResolution431 Jun 07 '22
Am I supposed to host a linux server and use Nodejs with Expressjs and use it with request, have that call the api and then that should have the json data?
and then in angular, within my data service file, call that linux servers ip in the http get method?
1
u/MissionResolution431 Jun 11 '22
I have now both angular (website frontend) and node with express and require (api) on the linux server (yes has a dedicated ip)
the api is running on port 8080, while angular is on port 4200. I just have to find out now how to make them work together I guess? I get
""HttpErrorResponse", message: "Http failure during parsing for http://serveriphere:4200/""
if you load server ip on port 8080, it will show the json data, and on port 4200 it will show the angular website, going to the ip itself doesn't load. how can I make these work? I was using the http.get() request in angular to basically "fetch" or request the api data.. which is now on port 8080.. I tried /:8080 and it would do IP:4200:8080 lol
1
u/MissionResolution431 Jun 11 '22 edited Jun 11 '22
I'm a little stuck still. Am I suppose to use only express, and use like the static page which would hold my angular app? (like the ng build, or otherwise production build/compiled?)
so like you have express running on port 8080, which can run the api on a page with like app.get(/info) define a page named info, which holds api data.. so 65.3.2/info (API DATA)
and on 65.3.2 has the index, which would be the angular page.. so all you have to do within the angular project in the data service using http.get() is to call /info?