简体   繁体   English

Cordova在Angular应用程序中构建更改请求URL

[英]Cordova build change request url in angular app

I've got problem with my application. 我的应用程序有问题。 I create app in angular and wrap with cordova. 我创建有角度的应用程序并用cordova包装。 In android simulator request url is good but when I copy files and test in my mobile phone, url is wrong. 在android模拟器中,请求url很好,但是当我复制文件并在手机中进行测试时,url错误。

My request: http://111.111.11.111/myReq - good 我的要求: http : //111.111.11.111/myReq-很好

On my phone, after Cordova build: file:///android_asset/www/null/login -bad 在我的手机上,在Cordova构建之后:file:/// android_asset / www / null / login -bad

And this my error: 这是我的错误:

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

I try the solution: 我尝试解决方案:

  1. ng build --prod and cordova build --prod ng build --prodcordova build --prod
  2. Rename files and directory to lowercase 重命名文件和目录为小写
  3. Add line: preference name="loadUrlTimeoutValue" value="60000" or 70000 to config.xml 将行:首选项名称=“ loadUrlTimeoutValue”值=“ 60000”或70000添加到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 由于我的假设是正确的,因此我将给出一个真实的答案,请创建另一个变量,该变量包含将接收POST请求的完整网址

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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