如何使用 ionic2 本机 http 插件发送 HTTP PUT 请求?

How to send HTTP PUT request using ionic2 native http plugin?

我正在尝试使用 http native plugin 从 ionic 2 应用程序发送 HTTP put 请求,但是,从文档中,我只看到 postget 请求方法已实现。

首先: 我想知道 http 方法 deleteput 是否没有真正实现(因为我可能没有找到正确的地方在文档中)。

其次: 如果没有实现,我们如何从 ionic 2 应用程序生成 putdelete?是否可以在低级别进行,而无需修改插件 native/library 代码?

提前致谢。

更新 1:putdeletepostJSON 实现的拉取请求 (https://github.com/wymsee/cordova-HTTP/pull/105),然而,从 2016 年 9 月到现在 2017 年 4 月,它没有与 master 合并。

更新2: 首先感谢@Jamil Hijjawi 的回答。那解决了问题。但顺便提一下,解决方案 2 需要一些服务器配置来绕过 CORS,所以我选择了第一个。还有一篇很好的文章 https://www.joshmorony.com/using-cordova-plugins-in-ionic-2-with-ionic-native/ 解释了如何使用 "Ionic Native" 中没有的 cordova 插件,我建议您阅读它。我想指出一个部分,如:

The way in which you use a non Ionic Native plugin in Ionic 2 is the same as the way you would use it in any Cordova project, which is however the plugin’s documentation says you should use it. However, there are some extra steps you need to take because TypeScript will cause some problems.

进一步说明,我们需要通过插件在 declarations.d.ts 之类的地方声明全局导出变量 cordovaHTTP,以便打字稿知道这一点。一旦声明,就不需要依赖注入。以下是我在随机组件函数中使用的代码片段。

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(
    public navCtrl: NavController,
    private _platform: Platform
  ) {
    this._platform.ready().then(() => {

      // ========== We need this part
      cordovaHTTP.put('http://192.168.0.5:8081/test-route', {
        really: "Yea!"
      }, {}, (res: any) => {
        console.log(res);
      }, (err: any) => {
        console.log(err);
      });
      // ========== till here, as for the example of usage
    });
  }

}

选项 1:

cordova-http插件只支持GETPOST,所以如果你想使用PUT直接从pull request repo的作者那里安装插件https://github.com/spuentesp/cordova-HTTP 使用此命令 ionic plugin add https://github.com/spuentesp/cordova-HTTP

但是 ionic-native 中的这些 API 没有实现声明,因此您可以在 declarations.d.ts 中声明 cordovaHTTP 命名空间并使用 API你想要

选项 2:

在angular

中使用Http服务
this.http.put(url, JSON.stringify(hero), {headers: headers}).map(res => res.json());

Http class 文档 https://angular.io/docs/ts/latest/api/http/index/Http-class.html