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 plicated.
How could be an easy example, with this criteria:
- Example URL:
- The request should be only in Javascript. It should be an async request and with the callback function.
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.
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 plicated.
How could be an easy example, with this criteria:
- Example URL: http://www.youtube./get_video_info?video_id=wbSwFU6tY1c
- The request should be only in Javascript. It should be an async request and with the callback function.
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.
Share Improve this question edited Oct 27, 2018 at 0:28 Baku Bakar asked Oct 26, 2018 at 23:37 Baku BakarBaku Bakar 4622 gold badges9 silver badges25 bronze badges 1- "But all of these solutions are plicated." Are they? Why not ask a question about what you're confused about. There are only two APIs you can use to make an HTTP request from JavaScript in-browser. AJAX and the Fetch API. – Brad Commented Oct 27, 2018 at 0:39
3 Answers
Reset to default 2Very simply
let request = new XMLHttpRequest();
request.open("GET", "http://www.example/example.txt", true);
request.onload = () => {console.log(request.responseText)}
request.send();
Take a look at https://developer.mozilla/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
You can try this https://developers.google./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);
});
You can use Superagent, lighter than jQuery and more patible than other solutions. You can also find some example