如何使用fetch
api javascript(https://github.com/github/fetch)传递查询字符串?
var url = "http://www.abcd.com";
var query = {
a: "test",
b: 2
};
当我将一些参数传递给http://www.abcd.com?a=test&b=2
fetch
答案 0 :(得分:6)
var params = Object.keys(query)
.map((key) => encodeURIComponent(key) + "=" + encodeURIComponent(query[key]))
.join("&")
.replace(/%20/g, "+");
fetch(url + "?" + params);
或使用options
对象 - but this will NOT work with GET
and HEAD
method:
fetch(url, {
method: "POST",
body: convertObjectToFormData(query)
}).then(...);
function convertObjectToFormData(obj) {
var formData = new FormData();
for (var key in obj) {
formData.append(key, obj[key]);
}
return formData;
}