0

I'm working on the frontend of a web application using Angular. When I send a GET request with Postman, it works properly, but if I send the same request from the frontend, it gives an error. Could you please give me a hint? Thank you!
This is the error I get:

core.js:6157 ERROR DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL
    at http://localhost:4200/polyfills.js:12125:35
    at XMLHttpRequest.proto.<computed> [as open] (http://localhost:4200/polyfills.js:10714:52)
    at Observable._subscribe (http://localhost:4200/vendor.js:122729:17)
    at Observable._trySubscribe (http://localhost:4200/vendor.js:24049:25)
    at Observable.subscribe (http://localhost:4200/vendor.js:24035:22)
    at DoOperator.call (http://localhost:4200/vendor.js:132235:23)
    at Observable.subscribe (http://localhost:4200/vendor.js:24030:31)
    at CatchOperator.call (http://localhost:4200/vendor.js:25097:23)
    at Observable.subscribe (http://localhost:4200/vendor.js:24030:31)
    at DoOperator.call (http://localhost:4200/vendor.js:132235:23)

This is part of my code:

getAllUsers(page: number, size: number, sortBy: string, sortDirection: string): Observable<IUserResponseFormatted> {
    const sort = [sortBy, sortDirection].toString();
    const currentUser: IUser = this.lsService.getItem('currentUser') as IUser;
    const token = "Bearer " + currentUser.accessToken;
    const headers = new HttpHeaders()
    .set('Authorization', token);
    return this.http.get<IUserResponse>(`${environment.SECURITY_URL}user?page=${page}&size=${size}&sort=${sort}`, {headers})
    .pipe(
      map((response: IUserResponse): IUserResponseFormatted => {
        const users: IUser[] = response._embedded.user.map((user: IUser): IUser => {
          return user;
        });
        const pageData = response.page;
        return { users, pageData }
      }),
      catchError(this.handleError)
    );
  }
3
  • Is it the first http call you're doing in your app ? Check that you have imported HttpClient from Angular and not from somewhere else. Also, be sure the called url is correct, with http:// or https:// at the beginning. stackoverflow.com/questions/42461174/… Commented Feb 21, 2021 at 17:55
  • @QuentinGrisel no, it's not the first call I'm doing in my app, so I'm sure that HttpClient is imported correctly. Also, I'm sure that the URL is correct, since it is the same that I used with Postman... Commented Feb 21, 2021 at 18:37
  • @lucia did you solve the Problem? Commented Sep 29, 2023 at 7:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.