【问题标题】:How to send data in request body in get request in Ionic http native?如何在 Ionic http native 的 get 请求中发送请求正文中的数据?
【发布时间】:2021-08-13 00:29:54
【问题描述】:

我需要在 Ionic Http native 的 get 请求的请求正文中发送一些数据。

我正在使用以下代码发送获取请求,但这会将数据转换为查询字符串。

this.http.get(
      'apiendpoint',
      data ,
      headers
    );
}

使用以下插件:

import { HTTP } from '@ionic-native/http/ngx';

以下是官方文档的链接:

https://github.com/silkimen/cordova-plugin-advanced-http

【问题讨论】:

  • 在这种情况下,您可以将数据发送到 post 方法而不是 get。 bcoz get 总是向 queryString 发送数据。

标签: ionic-framework ionic4 ionic-native


【解决方案1】:

如果你想发送数据,你应该使用 POST 方法,而不是 GET 方法:

import { HTTP } from '@ionic-native/http/ngx';

constructor(private http: HTTP) {}
...
    this.http.post(
          'apiendpoint',
          data,
          headers
        );
    }.then(data => {
    
        console.log(data.status);
        console.log(data.data); // data received by server
        console.log(data.headers);
    
      })
      .catch(error => {
    
        console.log(error.status);
        console.log(error.error); // error message as string
        console.log(error.headers);
    
      });

【讨论】:

    猜你喜欢
    • 2016-08-04
    • 2020-04-25
    • 2017-04-21
    • 1970-01-01
    • 2011-05-26
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多