[英]JSON.Stringify() returns undefined instead of text
I am making a form that downloads the data the user puts into it.我正在制作一个下载用户放入其中的数据的表单。 So far the file downloads but instead of the user's input it returns
[object Object]
到目前为止,文件已下载,但它返回的是
[object Object]
而不是用户的输入
I tried using JSON.Stringify() But it is returning a file with "undefined" inside it.我尝试使用 JSON.Stringify() 但它返回一个里面有“undefined”的文件。 Even though the console.log() is giving me
{username: "asdasd", password: "sdasdasd"}
即使 console.log() 给了我
{username: "asdasd", password: "sdasdasd"}
e.preventDefault();
console.log(formData);
var formDataString = JSON.stringify(FormData);
// ... submit to API or something
download(formDataString, 'json.txt', 'text/plain');
};
const initialFormData = Object.freeze({
username: "",
password: "",
});
function download(formDataString, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([formDataString], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
My full code is viewable here我的完整代码可以在这里查看
Thank you in advance for your help.预先感谢您的帮助。
If you check lines 2 and 3 of snippet reported here, the answer is pretty simple: you're saying JSON.stringify(FormData)
with capital F , while the console.log
outputs formData
with lower f如果您检查此处报告的代码段的第 2 行和第 3 行,答案非常简单:您说的是
JSON.stringify(FormData)
大写的F ,而console.log
输出的formData
f较低
Variables are case-sensitive in JavaScript, so FormData
is never defined ( undefined
), while formData
is what you have correctly defined above, and console.log(formData)
correctly outputs in the console the content of the variable. JavaScript 中的变量区分大小写,因此
FormData
永远不会被定义( undefined
),而formData
是你在上面正确定义的,并且console.log(formData)
在控制台中正确输出了变量的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.