如何通过GET请求通过fetch API发送数据?

时间:2015-12-31 08:44:43

标签: react-native fetch fetch-api

我想通过fetch API将一些数据发送到服务器,以便它可以根据它查询数据库。与Ajax不同,我无法找到任何合适的方法。 这是我的提取电话:

{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);
           }); 
  }
}

我想发送一个参数,例如电子邮件,以便服务器可以使用它查询数据库。我正在使用react-native android因此无法使用Ajax请求。 我只想使用GET方法。目前,服务器在req.query

中显示我在URL中传递的查询参数

3 个答案:

答案 0 :(得分:4)

GET请求不支持正文,并且必须在您的示例中显示的URL中发送参数。如果您无法在服务器端看到它们,则可能是服务器的问题应该作为单独的问题提出。

作为完整性测试,在使用fetch实现调用之前,在浏览器(或Postman之类的工具)中导航到该URL并测试结果是否正确,以便您至少确认是否&# 39;服务器问题或JavaScript问题。

答案 1 :(得分:0)

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,
  })
};

答案 2 :(得分:0)

尝试使用%40转义@字符或执行{fetch("http://192.168.90.53:4000/game/?email=" + encodeURIComponent('adicool2294@gmail.com'), {method: "GET"} )...

之类的操作
相关问题