可以在浏览器上运行但不能在Ionic DevApp上运行的离子应用程序

时间:2019-11-05 12:55:24

标签: angular rest ionic-framework ionic4 ionic-devapp

我是离子的新手,如果我的问题很愚蠢,请原谅我。 在开发过程中,我正在使用浏览器显示和测试我的应用程序,该程序一切正常,没有任何问题,但是当我在Android设备上的Ionic DevApp上测试我的应用程序时,该应用程序无法与后端进行通信( REST API)。每次发送请求(例如登录请求)时,系统都会响应[object progressevent]

console.log显示错误消息:

[ng] [console.log]: {
[ng]   "headers": {
[ng]     "normalizedNames": {},
[ng]     "lazyUpdate": null,
[ng]     "headers": {}
[ng]   },
[ng]   "status": 0,
[ng]   "statusText": "Unknown Error",
[ng]   "url": "http://localhost:8080/restapi/api/user/login",
[ng]   "ok": false,
[ng]   "name": "HttpErrorResponse",
[ng]   "message": "Http failure response for http://localhost:8080/restapi/api/user/login: 0 Unknown Error",
[ng]   "error": {
[ng]     "isTrusted": true
[ng]   }
[ng] }

我的应用程序:

前端: 离子+角形

后端(REST API): Java Spring MVC,Oracle开发人员数据库,Hibernate

我已经搜索并尝试在不同的在线资源上找到解决方案,但是不幸的是,其他人提供的解决方案在我的情况下不起作用。因此,我要发布此问题,以寻求离子领域专家的帮助。 谢谢。

我的登录请求代码:

this.http.login(this.user)
        .subscribe((userData: any) => {
                console.log('Success login');
                this.http.storeToken(userData.token);
                this.loginForm.reset();
                this.errorMessageService.setValidationErrorMessage('');
                this.exceptionHandlerService.setErrorResponse('');
                console.log(userData);
                this.router.navigate(['']);
            },
            error => {
                if (this.exceptionHandlerService.getErrorResponse() === null || this.exceptionHandlerService.getErrorResponse() === '') {
                    console.log(error);
                    this.exceptionHandlerService.setErrorResponse(error.error);
                }
                console.log(error);
            });

我的登录服务代码:

userUrl = 'http://localhost:8080/restapi/api/user/';
login(user: User) {
        const body = JSON.stringify(user);
        return this.http.post<User>(this.userUrl + 'login', body, httpOptions);
    }

1 个答案:

答案 0 :(得分:2)

感谢您的帮助,我找到了解决方法。

我将前端应用程序代码中的API服务URL从localhost更改为我的计算机ip地址,现在一切正常。

从:

userUrl = 'http://localhost:8080/restapi/api/user/';

收件人:

userUrl = 'http://192.164.0.111:8080/restapi/api/user/';

大声喊叫Reaz Murshed做出的回答: Cannot connect to localhost API from Android app