DEV Community

Rahul3229
Rahul3229

Posted on

How does Fetch works exactly?, and how can I use GET method with parameters?

Hi, I am new to React and I am trying to get specific search results based on a city name. I have a variable which is entirely dependent on user's input(city name) and I would like to use this variable as a parameter in my URL. I am sure my syntax is wrong and I tried googling, but I cannot understand anything. Can someone please explain how Fetch API works?, and how can I make a GET request with parameters?. Here's my code-

const City1= useSelector(state=>state.City); // using Redux
const myrequest= fetch('http://xxxx.xxxx.xxxx.xxxx/xxxx/:?{$City1}',{
method: 'GET',
headers: {'Access-Control-Allow-Origin':'*','Content-Type':'application/json'}
}).then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(userData => {
// Process the retrieved user data
console.log('User Data:', userData);
})
.catch(error => {
console.error('Error:', error);
});;
my error-
Failed to load resource: the server responded with a status of 404 (Not Found)
xxxx.js:66 Error: Error: Network response was not ok

Top comments (0)