Failed to execute 'open' on 'XMLHttpRequest' : invalid URL


#1

Hi,
I’m new in ionic 3 , I try to make a login post to API,
here is my api provider

export class ApiProvider {

  private host_url : string = '//api.someapihost.com:8080/app_dev.php';

  constructor(private http: Http) {
  }

  postLogin(data){
    let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
    let options = new RequestOptions({ headers: headers });
    let urlSearchParams = new URLSearchParams();
    urlSearchParams.append('username', data.username);
    urlSearchParams.append('password', data.password);
    let body = urlSearchParams.toString()
    return this.http.post(this.host_url+'/login_check/', body, options)
        .map(this.extractData)
        .catch(this.handleErrorObservable);
  }


  private extractData(res: Response) {
    let body = res.json();
    return body || {};
  }
  private handleErrorObservable (error: Response | any) {
    console.error(error.message || error);
    return Observable.throw(error.message || error);
  }
  

and this is my login.ts

 doLogin(){
    let loader = this.loadingController.create({
      content: "Sign in ...."
    });
    loader.present();
    this.api.postLogin(this.loginForm.value).subscribe(
        data => {
          console.log(data)
          loader.dismiss()

       
  }

it’s works fine on chrome but when I build in android device and try to test my login I got

Failed to execute ‘open’ on ‘XMLHttpRequest’ : invalid URL

when I debug on chrome inspect device console.
I also have installed whitelist plugin and have this meta-tag in my index.html
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

I try to figure out for hours, please help guys…


#2

console.log the URL you are trying to request before you actually do it.


#3

This is an invalid URL //api.someapihost.com:8080/app_dev.php because your android device add ‘//’ to URL .


#4

so I’ve to complete di URL like http:// or https:// ? let me try


#5

it works now thanks @guadyass and @Sujan12 :smiley: .
I usually using // for angularjs web app and it works, and now I know it will not work on ionic . Thank you guys