-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
axios put & FormData 实现 重复 key #116
Labels
axios put & FormData 实现 重复 key
axios put & FormData 实现 重复 key
Comments
const params = {
creative_id: "4838",
creative_word_ids: "",
description: ["zzz", 'abc'],
title: ["111", '222'],
};
// 1. Object 转 FormData
const formData = new FormData();
// ReferenceError: FormData is not defined ❓ Web API
for (const [key, value] of Object.entries(params)) {
if (Array.isArray(value)) {
for (const item of value) {
formData.append(key, item);
}
} else {
formData.append(key, value);
}
}
console.log('formData.toString() =', formData.toString());
for (const [key, value] of formData.entries()) {
console.log('key, value =', key, value);
}
/*
✅ 实现 重复 key (title / description)
formData.toString() = [object FormData]
key, value = creative_id 4838
key, value = creative_word_ids
key, value = description zzz
key, value = description abc
key, value = title 111
key, value = title 222
*/
// 2. FormData 转 URLSearchParams
const queryString = new URLSearchParams(formData);
console.log('queryString.toString() =', queryString.toString());
for (const [key, value] of queryString.entries()) {
console.log('key, value =', key, value);
}
/*
✅ 实现 重复 key (title / description)
queryString.toString() = creative_id=4838&creative_word_ids=&description=zzz&description=abc&title=111&title=222
key, value = creative_id 4838
key, value = creative_word_ids
key, value = description zzz
key, value = description abc
key, value = title 111
key, value = title 222
*/
|
ReferenceError: FormData is not defined ❓ Web API
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
axios put & FormData 实现 重复 key
The text was updated successfully, but these errors were encountered: