简体   繁体   中英

Cordova build change request url in angular app

I've got problem with my application. I create app in angular and wrap with cordova. In android simulator request url is good but when I copy files and test in my mobile phone, url is wrong.

My request: http://111.111.11.111/myReq - good

On my phone, after Cordova build: file:///android_asset/www/null/login -bad

And this my error:

POST file:///android_asset/www/null/login net::ERR_FILE_NOT_FOUND

I try the solution:

  1. ng build --prod and cordova build --prod
  2. Rename files and directory to lowercase
  3. Add line: preference name="loadUrlTimeoutValue" value="60000" or 70000 to config.xml

This is my request:

userLogin(login: string, password: string): Observable<any> {

    const baseUrl = 'http://111.111.11.111/myReq'

    const httpOptions  = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json',
        'Authorization': 'myToken',
      })
    };

    return this.http.post<any>(`${baseUrl}/login`, JSON.stringify({login, password}), httpOptions);

  }

and nothing work. How can I fix it?

Since my assumption was correct I'll give a real answer, create another variable that contains the full url that will receive the POST request

const requestUrl = `${baseUrl}/login` 

return this.http.post<any>(requestUrl, JSON.stringify({login, password}), httpOptions)

If for any reason the template variable is not working for you ( do a console log to check the value ), just concat the two strings like follow

const requestUrl = baseUrl + '/login'

return this.http.post<any>(requestUrl, JSON.stringify({login, password}), httpOptions)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM