let params = { "param1": "value1", "param2": "value2" };
let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
let url = 'https://example.com/search?' + query;
fetch(url)
.then(data => data.text())
.then((text) => { console.log('request succeeded with JSON response', text) })
.catch(function (error) { console.log('request failed', error) });
var data = { "KEY" : "VALUE" };
var url = new URL("http://site.com/API");
for (let k in data) { url.searchParams.append(k, data[k]); }
fetch(url);
fetch 결과를 json() or test() 처리를 해 줘야 한다. 그리고 await를 해 줘야 promise 객체가 벗겨진 값이 변수에 들어간다.
const result = await fetch("http://site.com/API")
.then((res) => res.json())
출처:
https://tristan91.tistory.com/611
https://code-boxx.com/javascript-fetch-get-query-params/
https://developer.mozilla.org/ko/docs/Web/API/Fetch_API/Using_Fetch