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