Convert json object to formData (HTML5 Object)

妖精的绣舞 提交于 2019-12-25 14:45:36

问题


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.

My code right now:

formdata = new FormData();
var uploadJson = {
  "default_lang": "en",
  "words": [
    {
      "desc": $scope.selectedWord,
      "enabled": true,
      "examples": $scope.examples
    }
  ]
};

formdata.append('file', uploadJson);

However, formdata is always empty even after appending uploadJson.

Does anyone know how to fix/do that?


回答1:


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



来源:https://stackoverflow.com/questions/43334542/convert-json-object-to-formdata-html5-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!