I'd like make a request on : https://jsonplaceholder.typicode.com/users but I get an Invalid URL error but the URL is ok to me. I really don't understand why.
Error message : Message: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL
export class User {
id: string;
name: string;
email: string;
username: number;
}
import { User } from '../models/user';
@Injectable({
providedIn: 'root'
})
export class UserService {
// REST API
endpoint = 'https://jsonplaceholder.typicode.com/';
constructor(private httpClient: HttpClient) { }
getUsers(): Observable<User> {
return this.httpClient.get<User>(this.endpoint + 'users')
.pipe(
retry(1),
catchError(this.processError)
)
}
processError(err) {
let message = '';
if(err.error instanceof ErrorEvent) {
message = err.error.message;
} else {
message = `Error Code: ${err.status}\nMessage: ${err.message}`;
}
console.log(message);
return throwError(message);
}
}
In the component :
export class UserListComponent implements OnInit {
Users: any = [];
constructor(private crudService: UserService) {}
ngOnInit() {
this.fetchUsers();
}
fetchUsers() {
return this.crudService.getUsers().subscribe((res: {}) => {
this.Users = res;
})
}
}
In the html
<table class="table">
<thead>
<tr class="table-primary">
<th>#User Id</th>
<th>Name</th>
<th>Us
er Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let data of Users">
<th scope="row">{{data.id}}</th>
<td>{{data.name}}</td>
<td>{{data.username}}</td>
<td>{{data.email}}</td>
</tr>
</tbody>
</table>
