xgqfrms / learning

learning : A collection of all kinds of resources, videos, pdf, blogs, codes... 📚 + 💻 + ❤
https://learning.xgqfrms.xyz
MIT License
16 stars 12 forks source link

axios put & FormData 实现 重复 key #116

Open xgqfrms opened 3 years ago

xgqfrms commented 3 years ago

axios put & FormData 实现 重复 key

image


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);
}

// 2. FormData 转 URLSearchParams
// const queryString = new URLSearchParams(forData);
// console.log('queryString.toString() =', queryString.toString());

// for (const [key, value] of queryString.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

*/
xgqfrms commented 3 years ago

https://www.cnblogs.com/xgqfrms/p/15162072.html

xgqfrms commented 3 years ago
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

*/
xgqfrms commented 3 years ago

ReferenceError: FormData is not defined ❓ Web API

  1. Web API , node.js 不支持 ❌
  2. 浏览器 OK ✅
xgqfrms commented 3 years ago

image

image