Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 14, 2023 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/e9d92b6caa7ae7a6c3346e8d8ce37306 to your computer and use it in GitHub Desktop.
Save code-boxx/e9d92b6caa7ae7a6c3346e8d8ce37306 to your computer and use it in GitHub Desktop.
Javascript Fetch With GET Query Params

JAVASCRIPT FETCH WITH GET PARAMETERS

https://code-boxx.com/javascript-fetch-get-query-params/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>JS Fetch With GET Query</title>
<script>
function fetchGet () {
// (A) DATA TO SEND
var data = {
name : "jon doe",
email : "jon@doe.com"
};
// (B) BUILD URL
var url = new URL("http://site.com/x-dummy.php");
for (let k in data) { url.searchParams.append(k, data[k]); }
console.log(url);
// (C) FETCH WITH GET SEARCH PARAMS
fetch(url)
// (D) RETURN SERVER RESPONSE AS TEXT
.then(res => {
console.log(res);
if (res.status != 200) { throw new Error("Bad Server Response"); }
return res.text();
})
.then(res => console.log(res))
// (E) HANDLE ERRORS (OPTIONAL)
.catch(err => console.error(err));
}
</script>
</head>
<body>
<input type="button" value="Fetch Get" onclick="fetchGet()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JS Fetch With GET Query</title>
<script>
function fetchGet () {
// (A) DATA TO SEND
var data = {
name : "jon doe",
email : "jon@doe.com"
};
// (B) MANUAL CONSTRUCT URL & PARAMS
var url = "http://site.com/x-dummy.php";
url += "?";
for (let k in data) { url += k + "=" + data[k] + "&"; }
url = encodeURI(url.slice(0, -1));
// (C) FETCH WITH GET SEARCH PARAMS
fetch(url)
// (D) RETURN SERVER RESPONSE AS TEXT
.then(res => {
console.log(res);
if (res.status != 200) { throw new Error("Bad Server Response"); }
return res.text();
})
.then(res => console.log(response))
// (E) HANDLE ERRORS (OPTIONAL)
.catch(err => console.error(err));
}
</script>
</head>
<body>
<input type="button" value="Fetch Get" onclick="fetchGet()">
</body>
</html>
<?php
print_r($_GET);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment