所以我试图弄清楚如何使用GET请求对XHR进行格式化,例如:
https:://api.com/endpoint?id=userId&list=list1%5D=1&list=list2%5D=1
我正在建立一个名为列表的对象,看起来像
const lists = {
'list1' : 1,
'list2': :1
}
我知道它可以像这样使用$ .ajax:
$.ajax({
url: 'https://api.com/endpoint',
dataType: 'json',
data: {
"type": "POST",
"id": userId,
"lists": lists
}
})
但是我需要使用香草JS。我尝试了以下方法:
const params = `type=POST&id=${userId}&lists=${lists}`
const xhr = new XMLHttpRequest()
xhr.open('GET', `https://api.com/endpoint${params}`);
xhr.send(encodeURI(params))
那只是试图达到:
https://api.com/endpoint&type=POST&id=userId&lists=[object%20Object]
所以我的主要目的是尝试获取URL来将列表参数格式化为
&list=list1%5D=1&list=list2%5D=1
我希望这是有道理的!
欢呼