将json对象转换为formData(HTML5对象) [英] Convert json object to formData (HTML5 Object)
本文介绍了将json对象转换为formData(HTML5对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到许多关于将formData转换为JSON对象的文章,但是,我有完全相反的用例.我有一个JSON对象,我想将其转换为formData对象,这是端点API所必需的.
I have seen many posts about converting formData to JSON object, however, I have the exact opposite use case. I have a JSON object which I would like to convert to a formData object as this is required by my endpoint API.
我现在的代码:
formdata = new FormData();
var uploadJson = {
"default_lang": "en",
"words": [
{
"desc": $scope.selectedWord,
"enabled": true,
"examples": $scope.examples
}
]
};
formdata.append('file', uploadJson);
但是,即使在附加了uploadJson之后,formdata也始终为空.
However, formdata is always empty even after appending uploadJson.
有人知道如何解决/做到这一点吗?
Does anyone know how to fix/do that?
推荐答案
尝试将 javascript对象 字符串化为json.
Try stringifying the javascript object to json.
formdata.append('file', JSON.stringify(uploadJson));
Note that JSON is a string data format and there is no such thing as a JSON object
这篇关于将json对象转换为formData(HTML5对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文