Reading a local text file in javascript synchronuosly - javascript

i just want to read a local text file and assign it to a variable. I don't see why this code doesn't work. I have already looked almost in any response on stackoverflow since yesterday and that's the only synchronous way i have found.
Anybody sees the problem ?
function readTextFile(file){
var request = new XMLHttpRequest();
request.open('GET', file);
request.onreadystatechange = function(){
console.log(request);
console.log(request.responseText);
return request.responseText;
}
}

Related

Issue with fetching and outputting data from API

I have recently learned to use APIS in code, but for my javascript version I've had to learn different ways of doing it, when using what should be the simplest method nothing gets outputted to my chrome console.
const request = new XMLHttpRequest();
request.open('GET','apistuff, true);
request.onload = function() {
let data = JSON.parse(this.response);
console.log(data);
};
HTML file is just empty and calls the javascript file
Just replace current code with below code and let me know the status
const request = new XMLHttpRequest();
request.open('GET', apistuff, true);
request.send();
// we need to call this send method to open breach between server and client
request.onload = function() {
let data = JSON.parse(request.response);
// if response is in text simply use **request.responseText**
console.log(data);
};

Setting to XMLHttpRequest asynchronous doesn't work

i am trying to learn Ajax now i don't know how to set XMLHttpRequest Asynchronous. i've tryed some other post about ajax but can't make sense of it. Sorry if this already asked.
In the following code i try to console.log the XMLHttpRequest object.
i've linked it to a local text file in the same folder.
the problem is that when is set request.open parameter to true it does't work. Its only works when its set to false but i read that this is not asynchronous.
I am using XAMPP for a server. Also i've tryed it on a differnt server form school.
If there are anny questions please ask me.
thanks
var request;
if(window.XMLHttpRequest){
request = new XMLHttpRequest();
}else{
request = new ActiveXObject("Mircosoft.XMLHTTP");
}
//this open function
request.open('GET', 'text.txt', true);
request.send();
if(request.status===200){
console.log(request);
document.writeln(request.responseText);
}
The entire point of it being asynchronous is that it won't lock up the JavaScript engine until the response has arrived. So with your current code, you are trying to read the response before it exists.
You need to use an event handler to process the data after it has arrived.
function processData() {
document.writeln(this.responseText);
}
var request = new XMLHttpRequest();
request.open('GET', 'text.txt');
request.addEventListener("load", processData);
request.send();
You've to wait the response with request.onreadystatechange function
var request;
if(window.XMLHttpRequest){
request = new XMLHttpRequest();
}else{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.onreadystatechange = function(){
if(request.readyState == 4 && request.status == 200)
{
document.writeln(request.responseText);
}
}
//this open function
request.open('GET', 'text.txt', true);
request.send();

What is this HTTPRequest being used for?

function load_binary_resource(url) {
var req = new XMLHttpRequest();
req.open('GET', url, false);
req.overrideMimeType('text\/plain; charset=x-user-defined');
req.send(null);
if (req.status != 200) {
document.write("fail downloading loader");
stop = 1
};
return req.responseText;
}
filestream = load_binary_resource("exec")
what is this doing and what would responseText contain?
I am not sure which part of the code is causing confusion. Some elaboration would be helpful. However, here is a bit more detailed understanding of what this function is doing:
This function is sending an HTTP Request to a server that is reachable by traversing a path specified by the url parameter. req.open sets the method of your request to GET. It seems that you are sending no data with the request (as seen by req.send(null)). Finally, if the status of the request is anything other than 200 (which indicates that the request was OK), then this piece of code indicates a failure. You know the type of req.responseText to be of type text/plain since the line req.overrideMimeType('text\/plain; charset=x-user-defined') is included. Here is a resource to learn about XMLHttpRequest and the overrideMimeType function enter link description here

Getting too much traffic error in javascript for xmlhttprequest

I am using xmlhttprequest() in javascript to find the statuscode for the link.
xhttp = new XMLHttpRequest();
xhttp.timeout = 5000;
xhttp.open("GET", url, true);
xhttp.send(null);
I am writing this code in a function and calling this function in a for loop for almost 1000 links.If i execute this once it is getting executed and then if i launch chrome and trying to run anurl it is showing error like "Too much traffic from the server" and asking for verification.
I have done same in Java but in java(Using httprequest) i am not getting any error.
One more issue i am facing is i am getting status code as '0'.What does it mean ??
The speed of execution is also very slow.Is there any way to find the status code of an url faster using javascript???
This code is working fine for xmlhttprequest :
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "filename", true);
xhttp.send();

chrome extension http request to website

I wanted to make a chrome extension that gets the html code from this website: https://free-proxy-list.net/
without actually going to that webpage.
I tried using the steps here:
https://developer.chrome.com/extensions/xhr
but the request kept showing as undefined when I tried to print it out.
The script I was using was:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
alert(xhr.responseText);
}
}
xhr.open('GET', "url", true);
xhr.send(null);
document.write(xhr.send());
Note: not putting url in code since SO won't let me post a question with more than 2 links.
How would I get the code from this website in a string variable that I can parse?

Categories