如何传递查询字符串获取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\u0026amp;b=2
解决方法
var params = Object.keys(query) .map((key) => encodeURIComponent(key) + "=" + encodeURIComponent(query[key])) .join("&") .replace(/%20/g,"+"); fetch(url + "?" + params);
或与选项对象 – 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 query) { formData.append(key,query[key]); } return formData; }