gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:03:39 24 4
gpt4 key购买 nike

我在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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com