Baku Bakar
Baku Bakar

Reputation: 462

JS get request to external URL

I would like to make a request to an external URL and work with the content of this URL. The content of the URL contains plain text, but it has around 50,000 characters.

So I did search a little bit and find keywords like: Ajax request, get request, Async / sync, callback etc.

But all of these solutions are complicated.

How could be an easy example, with this criteria:

Thank you.


Update:

Please note, the example URL changed to a new one.

When I run a GET request (for example with this Online GET request tool), than I am able to read the response content. It's not like downloading anything before, just displaying the content. And this is exactly what I want to do. Just write the 'response content' of this URL to a variable in JS and work with it later.

Upvotes: 0

Views: 14784

Answers (3)

Aldo Maria Landini
Aldo Maria Landini

Reputation: 157

You can use Superagent, lighter than jQuery and more compatible than other solutions. You can also find some example

Upvotes: 0

Ankush Sharma
Ankush Sharma

Reputation: 677

You can try this https://developers.google.com/web/updates/2015/03/introduction-to-fetch

fetch('https://api.lyrics.ovh/v1/shakira/waka-waka')
  .then(
    function(response) {
      if (response.status !== 200) {
        console.log('Looks like there was a problem. Status Code: ' +
          response.status);
        return;
      }

      // Examine the text in the response
      response.json().then(function(data) {
        console.log(data);
      });
    }
  )
  .catch(function(err) {
    console.log('Fetch Error :-S', err);
  });

Upvotes: 1

William Bright
William Bright

Reputation: 535

Very simply

let request = new XMLHttpRequest();
request.open("GET", "http://www.example.org/example.txt", true);
request.onload = () => {console.log(request.responseText)}
request.send();

Take a look at https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest

Upvotes: 2

Related Questions