我目前正在构建一个 ionic 应用程序,并在我的 iPhone 5s 上部署一个开发版本。我部署的版本与我在网络浏览器上部署的版本完全相同。 Web 浏览器上的版本运行良好,没有显示任何错误。
但是,当我在我的 iPhone 5s 上运行该应用程序时,出现以下错误:
Http failure response for http://localhost:8080/get-preferences: 501 Not Implemented
此错误是以下函数的结果:
//Logs that 'body' and 'this.headers' are correctly created
await this.http.post('/get-preferences', body, {headers: this.headers})
.toPromise().then(data => {
//Logs everything as a correct result on web
//The following line is what displays the above error through XCode through the iphone.
}).catch(err => console.log("Error getPreferences occured: ", (<Error>err).message));
}
我在 google 上搜索了一下,向 ionic.config.json
添加代理应该已经解决了 web 和 iphone 的这个问题,但手机版本无法正常工作。
这是我的 ionic.config.json
的内容:
{
"name": "ionic2-app-base",
"app_id": "",
"type": "ionic-angular",
"integrations": {
"cordova": {}
},
"proxies": [
{
"path": "/get-preferences",
"proxyUrl": "http://serverIp/get-preferences"
},
{
"path": "/register-vote",
"proxyUrl": "http:/serverIp/register-vote"
},
{
"path": "/create-user",
"proxyUrl": "http://serverIp/create-user"
}
]
}
我还需要在 iphone 上启用什么吗? 有用吗?
我正在使用 import {HttpClient, HttpHeaders} from '@angular/common/http';
发布请求的模块。
最佳答案
我认为您尝试使用地址 htt://localhost: 8080/... 或 http://127.0.0.1:8080/ 从模拟器 (ios/android) 访问 API ...
不正常是正常的 localhost 是手机(甚至是模拟的)。
解决方案:将您的 api url localhost 更改为您的 pc/服务器示例的 ip 地址:192.168.xx.xx
关于ios - Http 失败响应 501 : Not implemented, 完全相同的方法适用于网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47496216/