1

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

7
  • 2
    What's the specific issue? Is there something in the RN docs or the MDN docs that doesn't make sense? Commented Nov 6, 2021 at 18:03
  • I'm not sure where I should put the pin: p, would it be in body the first parameter? Commented Nov 6, 2021 at 18:46
  • Can you just try it? The fetch docs are pretty clear (IMO, anyway :) regarding parameters, as are the jQuery docs—it’s not clear what specifically you’re having a problem with. Commented Nov 6, 2021 at 19:33
  • Well I tried it, I got this: const dataToSend = {pin: data}; fetch('192.168.0.129:80', { method: 'POST', headers: { 'Content-Type': 'application/urlencoded' }, body: JSON.stringify(dataToSend), }); But it doesnt work... It makes connection with the server but the data isn't send the same way as the website sends it. Commented Nov 6, 2021 at 19:52
  • Your title specifically asks about a GET request, but it looks like the code you're trying is a POST request. Commented Nov 6, 2021 at 20:13

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

I'm pretty sure this would've worked aswell!
If you are using an async function there is no need to use "then"
it's not about "need", it's about writing the res.json() in a single line. You can also write a customised fetch in a separate function so you do not need to write a await response.json() code.
0

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

3 Comments

This seems like a GET request your making with query params
I am realy confused, I know GET is to ask for data and POST is to send data but you can send data with a GET request?
Yes. Please have a look at MDN doc here : developer.mozilla.org/fr/docs/Web/API/Fetch_API/Using_Fetch. You'll find out more about 'method' argument that can be passed in order to perform 'POST' request with fetch ('GET' being the default mode)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.