angular - 如何将有效负载转换为 formData
问题描述
有没有一种方法可以将有效负载 json 对象转换为 formData ?const formData: FormData = new FormData();
请检查下面我的有效负载代码我已添加它
#有效负载输出
{
"id": 0,
"name": null,
"dealType": "Partner Location Submission",
"annualRentProposed": null,
"annualRentCurrent": null,
"firmTermRemaining": null,
"firmTermAdded": null,
"maxAvailableTerm": null,
"status": null,
"capitalContribution": null,
"parentCloneId": null,
"accountId": 4,
"transactionId": 173,
"dealTypeValues": "{\"id\":0,\"summary\":\"13123\",\"mlasId\":2,\"startDate\":\"2021-10-27\",\"endDate\":\"2021-10-27\",\"rent\":\"2321\",\"cam\":\"312\",\"securityMonitoringMonthly\":\"12312\",\"supportServicesFee\":\"312\",\"estimatedOtherRevenue\":\"12312\",\"descriptionOfOtherRevenue\":\"3123123\",\"totalMonthlyRentAndFees\":15380,\"buildOutCostReimbursement\":\"123123\",\"dealId\":0,\"startDateString\":\"2021-10-27\",\"endDateString\":\"2021-10-27\"}",
"isReadyForApproval": false
}
#有效载荷代码
const payload = {
"id": 0,
"name": this.dealPLSFormFields.dealName,
"dealType": "Partner Location Submission",
"annualRentProposed": null,
"annualRentCurrent": null,
"firmTermRemaining": null,
"firmTermAdded": null,
"maxAvailableTerm": null,
"status": null,
"capitalContribution": null,
"parentCloneId": null,
"accountId": this.currentAccount.accountId,
"transactionId": this.transactionData.id,
"dealTypeValues": JSON.stringify(dealTypeValues),
"isReadyForApproval": this.isReadyForApproval
}
解决方案
您可以使用映射对象键将对象转换为 FormData:
const myObj = {a: 1, b: 2};
const formData = new FormData();
Object.keys(myObj).forEach(key => {
formData.append(key, myObj[key]);
});
推荐阅读
- c++ - wxFrame 缺少最大化按钮
- javascript - 将 REST API 响应重定向到 UI/浏览器 NodeJs/Express/Request
- python - 如何使用 aws 胶水使用 s3 进行存储?
- unity3d - HoloLens - 如何从 Vuforia 获取网络摄像头纹理 2D
- hadoop - Hive 查询显示很少有减速器被杀死,但查询仍在运行。输出是否正确?
- javascript - 在 react-native-svg-uri 中加载本地 svg 文件,但它没有显示在正确的位置
- javascript - 从 markdown 生成 HTML 仅适用于 React App 中的 dangerouslySetInnerHTML
- python - 如何转换数据框以获取各种事件发生的时间?
- java - 合并两个 docx 文件:项目符号列表消失
- julia - 将插值函数保存到 Julia 中的单独文件中