我有一个 node.js 和 Ionic (angular) 应用程序,当我尝试从我的手机进行 API 调用时,我收到了这个错误 -> object XMLHttpRequestProgressEvent我正在像这样服务我的 Ionic 应用程序ionic serve --address=10.0.0.107。它不适用于来自客户端的每个电话仅在电话上,当我从桌面拨打电话时,一切都很好。这是来自 auth.service.ts 的登录部分 loginUser(u: User): Observable<any> { const url = `${this.baseUrl}/auth`; const body = { username: u.username, password: u.password }; // When you want to send a cookies with requests, // you have to put "withCredentials: true" into request option return this.http.post(url, body, this.noAuthOptions); }这是我的选择: private readonly baseUrl = "http://localhost:3000"; private readonly contentType = { "Content-Type": "application/json" }; private readonly noAuth = { "No-Auth": "True" }; private readonly header = new HttpHeaders(this.contentType); private readonly noAuthHeader = new HttpHeaders({ ...this.contentType, ...this.noAuth }); private readonly options = { headers: this.header, withCredentials: true }; private readonly noAuthOptions = { headers: this.noAuthHeader, withCredentials: true };这是我在 github 上的代码(前端)https://github.com/TenPetr/dashboard 和后端部分:https ://github.com/TenPetr/dashboard_backend你有什么想法,为什么会这样?
1 回答
holdtom
TA贡献1805条经验 获得超10个赞
问题是这一行:
private readonly baseUrl = "http://localhost:3000";
它可以在 PC 上运行,因为它通过 hosts 文件(位于 C:\Windows\System32\drivers\etc\hosts 中)知道此地址
但是,移动设备在本地没有此文件,因此无法解析 url。
您需要运行服务器并将 localhost 替换为您的 IP 地址。 https://ngrok.com/docs#getting-started
添加回答
举报
0/150
提交
取消
