Skip to content
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

Open
xgqfrms opened this issue Aug 19, 2021 · 4 comments
Open

axios put & FormData 实现 重复 key #116

xgqfrms opened this issue Aug 19, 2021 · 4 comments
Labels
axios put & FormData 实现 重复 key axios put & FormData 实现 重复 key

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented Aug 19, 2021

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
Copy link
Owner Author

xgqfrms commented Aug 19, 2021

@xgqfrms
Copy link
Owner Author

xgqfrms commented Aug 19, 2021

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
Copy link
Owner Author

xgqfrms commented Aug 19, 2021

ReferenceError: FormData is not defined ❓ Web API

  1. Web API , node.js 不支持 ❌
  2. 浏览器 OK ✅

@xgqfrms
Copy link
Owner Author

xgqfrms commented Aug 19, 2021

image

image

@xgqfrms xgqfrms added the axios put & FormData 实现 重复 key axios put & FormData 实现 重复 key label Aug 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
axios put & FormData 实现 重复 key axios put & FormData 实现 重复 key
Projects
None yet
Development

No branches or pull requests

1 participant