android - 如何使用 ionic 3 @ionic-native/http 在 native HTTP 中传递 FormData?

标签 android ionic-framework ionic3

我在ionic 3项目中添加了插件

$ ionic cordova plugin add cordova-plugin-advanced-http
$ npm install --save @ionic-native/http@4

我想在 native HTTP post 请求中传递 FormDate。

var formData = new FormData();
formData.append('file', fileObject)
formData.append('temp_id', '');
let headers = {
  "Accept": "application/json",
  "api-auth": 'apiAuthToken String',
  "User-Auth": 'userAuthToken String'
  }
this.http_native.setDataSerializer('urlencoded');
this.http_native.setHeader('*', 'Content-Type', 'application/x-www-form-urlencoded');
this.http_native.post('url String', formData, headers).then(api_response => {

});


错误:advanced-http:“data”参数仅支持以下数据类型:Object

最佳答案

你需要在post方法的第二个参数中传递一个对象。该对象将自动转换为您设置的 form-url-encoded -

this.http_native.setDataSerializer('urlencoded');

删除:

var formData = new FormData();
formData.append('file', fileObject)
formData.append('temp_id', '');

替换为:

var formData = {
    file: fileObject,
    temp_id: ''
 }

最后发出post请求:

let headers = {
  "Accept": "application/json",
  "api-auth": 'apiAuthToken String',
  "User-Auth": 'userAuthToken String'
  }
this.http_native.setDataSerializer('urlencoded');
this.http_native.setHeader('*', 'Content-Type', 'application/x-www-form-urlencoded');
this.http_native.post('url String', formData, headers).then(api_response => {

});

如果您想上传文件,请查看此文档 - https://github.com/silkimen/cordova-plugin-advanced-http#uploadfile

关于android - 如何使用 ionic 3 @ionic-native/http 在 native HTTP 中传递 FormData?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57773574/

相关文章:

Android 应用程序 onCreate() 和 Intent.ACTION_PACKAGE_REPLACED

javascript - 在 Ionic 应用程序的 AngularJS 中显示数据时出错

css - Ionic-Toggle 每侧都有标签

android - 即使在实现 dats 消息后,FCM 推送通知也会显示原始 json

android - 可以使用 Netbeans 开发 Android 3.x 应用程序吗?

android - 无法解析符号 PhoneNumberUtil

css - 如何在 Ionic 2 中将文本换行到 <ion-segment-button> 中

javascript - 无法更改登录和注销时的侧菜单 - Angular Ionic

angular - ionic 2 中带有异步管道的重复 http 请求

angular - 如何在 ionic 3 中单击图标时显示输入字段