简体   繁体   English

如何在本机反应中发出 POST 请求

[英]How can I make a POST request in react native

So I created a website where I used JS and jQuery to send data to a server.所以我创建了一个网站,在那里我使用 JS 和 jQuery 将数据发送到服务器。 But now I am making that website as an app on my phone using react native.但是现在我正在使用 React Native 将该网站制作为手机上的应用程序。 I've read about the fetch function but I don't completely understand how I would go about it to make this request.我已经阅读了 fetch 函数,但我不完全明白我将如何进行这个请求。 This is the code I used on my website:这是我在我的网站上使用的代码:

$(".btn").click(function() {
    var p = $(this).attr('id');
    pin: p
    $.get("http://192.168.0.129:80/", {
    pin: p
    });
    DisableButtons();
});

Right now I have the following:现在我有以下几点:

sendData = (data) => {
    console.log(data);
    var p = data;
    pin: p
    $.get("http://192.168.0.129:80/", {   --> this needs to be changed so it could work 
    pin: p                                    in react native 
    }); 
  }

So what I want to accomplish is to send this url when I call the function: http://192.168.0.129/?pin=xxx所以我想要完成的是在调用函数时发送这个url: http : //192.168.0.129/?pin=xxx

Thanks in advance提前致谢

A typical fetch request in javascript looks like this. javascript 中典型的 fetch 请求如下所示。

const sendData = async(data) => {
    const response = await fetch(`http://192.168.0.129:80/?pin=${p}`).then(res => res.json()) //or res.text() if you are expecting text response.
    console.log('results')

}

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

So I got the solution, it was simpler then I thought:所以我得到了解决方案,它比我想的更简单:

sendData = (data) => {
    console.log(data);
    var url = `http://192.168.0.129:80/?pin=${data}`;
    fetch(url);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM