DMCA.com Protection Status

How do I make a JavaScript send GET request with parameters?

You can make a GET request with parameters in JavaScript using the fetch() method. Here’s an example:

const url = 'https://example.com/data';
const params = {
  id: 123,
  name: 'John Doe'
};
const queryParams = new URLSearchParams(params);

fetch(`${url}?${queryParams}`)
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));

In this example, we’re using the fetch() method to make a GET request to the url with parameters id and name. The URLSearchParams class is used to create a new instance of the query parameters object, and we pass it to the fetch() method.

The fetch() method returns a promise, which resolves to the response object. We then use the json() method on the response object to parse the response body as JSON data.

Finally, we log the parsed data to the console. If there’s an error during the fetch, we catch it and log it to the console.

Read More  Android 15 Beta 1: Latest News and Revealed Features! Click Here to Find Out"
DMCA.com Protection Status