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();
Related
I am desperatly trying to code a plugin for Draw.io that will basically do a request to another website in order to get some images.
It works fine on the local web version of Draw.io, but on the desktop version I get an empty responseText.
Here is what I'm simply doing in the plugin js:
Draw.loadPlugin(function(ui)
{
var data = null;
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://jsonplaceholder.typicode.com/posts");
xhr.setRequestHeader("accept", "application/json");
xhr.send();
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
alert(this.responseText);
}
});
});
That is pretty basic and probably not working well but I should at least get an error... All I have is an empty response.
Does the Electron app blocks outside XHR? Is there a workaround? Thanks!
I am not able to make a post-call using xmlhttprequest in javascript. First, I am getting a 401 response code from server-side. Below is code I am using below code.
function makeRestCall(){
console.log("Rest call made")
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
xhttp.open("POST", "myurk", false, 'username', 'password');
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send()
alert(xhttp.status);
alert(xhttp.responseText)
I am also getting
"Request has been blocked by CORS policy"
I am able to do a rest call from postman and using basic auth and getting response also
I have referred various links for making a rest call using javascript.
Link 1 Link 2
But still, I am not able to resolve the error. Can anyone tell what is wrong am I doing.
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();
Django/Python server-side dev here. And also a JS newbie (8 days old). This question is about Ajax-initiated page reloads.
Here's a JS snippet where I'm processing the JSONResponse from a Django view I POSTed some data to. Depending on success or failure, you'll see that I'm calling a redirect to another Django view:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200)
{
var resp = JSON.parse(this.responseText);
if (!resp.success) {
console.log("Failed!");
} else {
console.log("Success! Redirect to: ", resp.success_url);
window.location.href = resp.success_url;
window.location.reload();
}
}
};
xhr.open('POST', e.target.action);
xhr.setRequestHeader("X-CSRFToken", get_cookie('csrftoken'));
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.send(form_data);
Specifically, the following lines do the trick:
window.location.href = resp.success_url;
window.location.reload();
My problem is that when this gets executed in Firefox, I am made to see the following dialog box:
It seems the POST parameters are being sent again ?! How do I just do a plain old GET type redirect to resp.success_url instead? Please advise and let me know if you need more information.
Note: let's stick to pure JS for the scope of this question - I'm learning vanilla JS these days and don't want to get into JQuery prematurely.
Try
window.location.replace(resp.success_url);
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?