const params = new URLSearchParams({
param1: 'value1',
param2: 'value2'
}).toString();
该方法可将param1和param2拼接为
param1=value1¶m2=value2
实例
const url = 'https://example.com/api/resource';
const params = new URLSearchParams({
param1: 'value1',
param2: 'value2'
}).toString();
const data = {
key1: 'value1',
key2: 'value2'
};
fetch(`${url}?${params}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
标签:const,请求,URLSearchParams,data,参数,value2,value1,new,地址栏 From: https://www.cnblogs.com/Simoon/p/18676588