0

I am trying to make a GET request to a web API using what is inputted in a textbox. So far I have the input value established but can't seem to figure out to send the request.

      const inputdata = document.getElementById('request');
      const requestdata = inputdata.value;
      console.log(requestdata);

This function correctly but I can't seem to figure out the rest.

I am trying to do the following:

https://api.example.com/request?=${requestdata}
or
https://api.example.com/request/${requestdata}/test

Keep in mind that this is a static HTML site with no Node

1 Answer 1

1

Something like this should work to make an asynchronous GET request:

function httpGetAsync(theUrl, callback)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            callback(xmlHttp.responseText);
    }
    xmlHttp.open("GET", theUrl, true);
    xmlHttp.send(null);
}

const inputdata = document.getElementById('request');
const requestdata = inputdata.value;

httpGetAsync("https://api.example.com/request?=${" + requestdata + "}");
<input type="text" id="request" value="test">

If you check the developer tools > network tab you should see the GET request to the API endpoint.

Example

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

2 Comments

This works great, but I keep getting an error because of the .send(null). Am I doing something wrong?
It should be null for GET requests. You can see more info here: developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send. What error(s) are you getting?

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.