【发布时间】:2023-01-12 13:39:57
【问题描述】:
我正在尝试使用 fetch 将输入字段的数据发送到后端。我使用 formData.append 来合并数据。当提取在后端运行时,我得到空列表。
async function autosave()
{
let formdata =new FormData();
let blogImage = document.querySelector("#blog_image").files[0];
let imageName = document.querySelector("#blog_image").files[0].name;
let blogTitle = blog_title.value.trim();
let contentType = document.querySelector("#content_type").checked;
let blogId = document.querySelector("#id_val").value;
let blogContent = editorbody.innerHTML;
// console.log(document.querySelector("#blog_image").files[0])
formdata.append("blog_image", blogImage, imageName)
formdata.append("blog_title", blogTitle);
formdata.append("content_type", contentType)
formdata.append("blog_id", blogId);
formdata.append("content",blogContent)
await fetch("/blog/autosave/",{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'mode':'no-cors'
},
method: "POST",
body:JSON.stringify(formdata)
}).then((res) => {return res.json()})
.then((data) => {
if (data.status == 200){
// function savesuccessFun();
console.log("Blog saved successfully");
}
else{
// savefailFun();
console.log("Opps blog can not be saved");
}
}).catch(err => console.log(err));
我只知道前端。在后端我们使用data = json.loads(request.body)获取数据
笔记:我法师不应该以 base64 格式发送。 所有变量都从表单字段中获取数据。
【问题讨论】:
标签: javascript fetch