The most common methods to send an ajax request from JS are:
The fetch api (quite new maybe not all browsers support it)
fetch('url')
.then(response => response.text()) // response is text format
.then(content => {
/* do something here*/
alert(content))
}
)
And the old XMLHttpRequest class
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
xhttp.open("GET", "url", true);
xhttp.send();
Note:
Sending AJAX requests on the main thread is a bad idea, it freezes the website, so you will get the data on an other thread
The remote server might not allow you to get its content from a different page, in this case the JavaScript console will show an error, like:
Access to XMLHttpRequest at 'https://google.com/' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
And there are some other less strict restriction like this, eg.: no fetching from http to https, all failed requests will be logged to console
To split a string into multiple parts use
const splitedArray = 'a b c'.split(' ' /* seperate by space */)
splitedArray[0] == 'a'
splitedArray[1] == 'b'
splitedArray[2] == 'c'