Using Query Parameters in a GET Request
Show how to append query parameters to the URL for a GET request to a cross-origin resource.
const params = new URLSearchParams({
search: 'query',
page: '2'
});
// Appends query parameters to the URL
const urlWithParams = `https://example.com/api/items?${params}`;
// Makes a GET request to the cross-origin resource
fetch(urlWithParams)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
This code snippet demonstrates how to create a URLSearchParams object with desired query parameters, append it to a base URL, and then make a GET request to a cross-origin resource. The fetch API is used here to perform the request and handle the response or error.