使用 axios.post 发送 FormData 并提交表单
该代码使用 axios 库发送一个 POST 请求。请求的 URL 是变量 url,请求体是变量 formData,请求配置包括 withCredentials 为 true 和请求头包括 Authorization 和 Content-Type。
请求成功后,代码创建一个名为 name 的变量,并调用 openWindow 函数打开一个窗口。然后,代码创建一个临时的 HTML 表单元素,并设置其 id、method、action 和 target 属性。
接下来,代码创建四个隐藏的 input 元素,并分别设置其 name 和 value 属性。然后,将这四个 input 元素添加到临时表单元素中。
接着,代码将临时表单元素添加到文档的 body 中,并调用 submit 方法提交表单。最后,将临时表单元素从文档的 body 中移除。
如果请求失败,代码会打印错误信息,并注释掉了一些代码,包括定义 name 变量和调用 openWindow 函数的部分。
总的来说,该代码通过 axios 发送一个 POST 请求,并在请求成功后打开一个窗口并提交一个表单。
axios.post(
url,
formData,
{
withCredentials: true,
headers: {
Authorization: getToken(),
'Content-Type': 'application/x-www-form-urlencoded',
},
}).then(res => {
const name = 'オリジナルモーダル';
openWindow(name);
var tempForm = document.createElement('form');
tempForm.id = 'tempForm1';
tempForm.method = 'post';
tempForm.action = url;
tempForm.target = name;
// 連携元システム区分
var hideInput = document.createElement('input');
hideInput.type = 'hidden';
hideInput.name = 'sourceSystemType';
hideInput.value = sourceSystemType;
// 連携元企業識別コード
var hideInput1 = document.createElement('input');
hideInput1.type = 'hidden';
hideInput1.name = 'sourceCompanyCode';
hideInput1.value = sourceCompanyCode;
// MCBリプレイス対応 #8906 start
// 初期表示用検索条件
var hideInput2 = document.createElement('input');
hideInput2.type = 'hidden';
hideInput2.name = 'initialDisplaySearchCriteria';
hideInput2.value = form.values.originalQuestionObj ? JSON.stringify(newJSON) : '';
// 提出物内容IDリスト
var hideInput3 = document.createElement('input');
hideInput3.type = 'hidden';
hideInput3.name = 'submissionContentIdList';
hideInput3.value = String(submissionsContentIdList);
// MCBリプレイス対応 #8906 end
//26P #80795 start
var hideInput4 = document.createElement('input');
hideInput4.type = 'hidden';
//26P #80795 end
tempForm.appendChild(hideInput);
tempForm.appendChild(hideInput1);
tempForm.appendChild(hideInput2);
tempForm.appendChild(hideInput3);
document.body.appendChild(tempForm);
tempForm.submit();
document.body.removeChild(tempForm);
}).catch(error => {
console.log(error);
// const name = 'オリジナルモーダル';
// openWindow(name);
});
原文地址: https://www.cveoy.top/t/topic/me10 著作权归作者所有。请勿转载和采集!