Home > OS >  How can I make a POST request in react native
How can I make a POST request in react native

Time:11-07

So I created a website where I used JS and jQuery to send data to a server. But now I am making that website as an app on my phone using 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. 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

Thanks in advance

CodePudding user response:

A typical fetch request in javascript looks like this.

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

CodePudding user response:

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);
  }
  • Related