How can I send data via the Fetch API on a GET request?

I want to send some data via the Fetch API to the server so that it can query the database based on this. Unlike Ajax, I cannot find a suitable method for this. Here is my request:

{fetch("http://192.168.90.53:4000/game/?email=adicool2294@gmail.com", {method: "GET"} )
        .then((response) => response.json())
        .then((responseData) => {
            id= responseData.id;
            name= responseData.name; 
            console.log(responseData);
        })
       .catch((error) => {
             console.warn(error);
           }); 
  }
}

I want to send a parameter such as an email address so that the server can query the DB using this. I am using reactive android, so I cannot use the Ajax request. I want to use only the GET method. Currently, the query parameters that I pass in the URL are not displayed by the server in req.query

+4
source share
2 answers

GET , URL-, . , , .

, URL- ( , Postman), , , , , , JavaScript.

+4
You can add that like below

fetch("http://192.168.90.53:4000/game", obj)  
.then(function(response) => response.json())
.then((responseData) => {
  id = responseData.id;
  name = responseData.name; 
  console.log(responseData);
},
.catch((error) => {
  console.warn(error);
}); 

var obj = {  
  method: 'POST',
  body: JSON.stringify({
    'email': your_email_here,
  })
};
0

Source: https://habr.com/ru/post/1622326/


All Articles