Angular 2 Failed to execute open on XMLHttpRequest: Invalid URL
If it's a 192.168..
type of url you should add a http://
or https://
in front of it. And you may need to enable CORS options on server side.
Another context when this error might happen is when using interceptors to set a base path. Something like the following:
@Injectable()
export class WithCredentialsInterceptor implements HttpInterceptor {
constructor(private logger: LoggingService) {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// one way is to except / include only some urls by checking request.url
const clonedRequest = request.clone({
withCredentials: true
});
// this.logger.logTrace("Sending with credentials request: ", request.urlWithParams);
return next.handle(clonedRequest);
}
}
If this interceptor fires when a request to a full http/https url is provided to the http client, it will construct an invalid URL.
if you use localhost then only use http:// before your localhost
In my case, I was able to solve it by fixing the url for my http request from the client side instead of the serverside as echonax mentioned from missing http://
from localhost
.
My issue was, in my request this.http.get(this.domain + '/api/prod/' + id).map(res => res.json())
I was missing a /
from the front side of api
. It should be /api