Search code examples
angularhttptypescriptget

Http Get returns Error


I have an angular component which should do a simple HTTP Get:

this.http.get<any>('http://localhost:80/userservice/login', { params: { username: this.model.username, password: this.model.password } })
        .subscribe(
        data => {
            this.router.navigate(['/register']);
        },
        error => {
            this.alertService.error(error.message);
            this.loading = false;
        });

And a webservice in Java which should response to the request:

@GET @Produces( MediaType.APPLICATION_JSON ) @Path("/login")
public Response login(@QueryParam("username") String username, @QueryParam("password") String password)
{
   return Response.status(Response.Status.OK).header("Content-type", "application/json").entity("test").build();
}

But Angular always goes into the error section of the subscription instead of the data subscription.

Error-Message: "Http failure response for (unknown url): 0 Unknown Error”

I already checked with breakpoints that my Java Server successfully receives the requests and then sends back the response.

What am I doing wrong? Thanks for any help!


Solution

  • The Problem was that:

    Access-Control-Allow-Origin='*'
    

    was missing in the Header. This prevented Angular accessing the Response.