OGeek|极客世界-中国程序员成长平台

标题: ios - Http 失败响应 501 : Not implemented, altought 完全相同的方法适用于 web [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:22
标题: ios - Http 失败响应 501 : Not implemented, altought 完全相同的方法适用于 web

我目前正在构建一个 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'; 模块进行发布请求。



Best Answer-推荐答案


我认为您尝试使用地址 htt://localhost: 8080/... 或 http://127.0.0.1:8080/ 从模拟器 (ios/android) 访问 API ...

不工作 localhost 是手机是正常的(即使是模拟的)。

解决方案:将您的 api url localhost 更改为您的 pc/server 的 ip 地址,例如:192.168.xx.xx

关于ios - Http 失败响应 501 : Not implemented, altought 完全相同的方法适用于 web,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47496216/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4