I want to reload the current page with javascript if a specific url is doing a redirection.I don't need to know the redirection url.The xhr request is being performed and i do get 302 redirect when i inspect the request but my specific redirection isn't happening.
var xhr = new XMLHttpRequest();
xhr.open('HEAD', 'https://www.filterbypass.me/s.php?k=BbrSU%2F%2F39m5gKhLDr31ZqjKYgrAdALscySz%2B6YarDYTH5G9hSDc%2B&b=4&f=norefer');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 301 || xhr.status == 302) {
//url expired , reload
console.log('perform redirection');
window.location.replace('https://www.google.com');
}
}
};
xhr.send();
I don't see the console.log and i already tried with xhr.readyState == 2.
Related
Is it possible to make a POSt request from a static website hosted on S3? Are there any workarounds?
Totally possible, just use XMLHttpRequest or add some lib (jquery) to help you with that:
<script>
var xhr = new XMLHttpRequest();
xhr.open("POST", '/server', true);
//Send the proper header information along with the request
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
// Request finished. Do processing here.
}
}
xhr.send("foo=bar&lorem=ipsum");
// xhr.send(new Int8Array());
// xhr.send(document);
</script>
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send
I'm making a simple POST XMLHttpRequest, it is successfully posting the data but also the paylod getting appended to the URL. Is there anyway the data doesnt appended to the URL
var payLoad= JSON.stringify(ItemJSON);
var xmlhttp = new XMLHttpRequest();
xmlhttp.addEventListener("readystatechange", function () {
if (xmlhttp.readyState < 4){
}
else if (xmlhttp.readyState === 4) {
if (xmlhttp.status == 200 && xmlhttp.status < 300){
console.log("Success",xmlhttp.responseText);
}
}else{
alert("Error!! \\n There was an error while saving. Please retry again later.");
}
});
xmlhttp.open("POST", URL, false);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(payLoad);
}
Response :
https://sample.com/api/apipipelines?apiName=asdasdaaaaaa&orgUnit=ASA&apiProductName=asdasd&leadDeveloperName=Sandeep&leadDeveloperEmail=sa11as1%40sandsssy.com&baseUrl=%2Fapplication%2Foip%2Fv1%2Fcase-management%2F&projectOwnerName=alisa&projectOwnerEmail=sa%40sasssssndy.com
It is because the header that you used while posting the content:
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
Change it to
xmlhttp.setRequestHeader("Content-Type", "application/json");
Reference for you: What is the difference between form-data, x-www-form-urlencoded and raw in the Postman Chrome application?
var xmlHTTP = new XMLHttpRequest();
xmlHTTP.open('GET','http://example.com/',true);
xmlHTTP.send();
I just keep getting XHR failed loading: GET, can anyone help???
Thanks
If the server your contacting doesn't have CORS set in the header (e.g. access-control-allow-origin: *), you will not be able to make the request. If you don't have access to the server to set a CORS header, you'll need to contact the server from a server you do control and then pass that to the browser (either with a CORS header or not if it's served from the same domain)
But your code works fine. The problem is with your http://example.com/ not returning
http://jsbin.com/zuhobidako/edit?html,js,console,output
var xmlHTTP = new XMLHttpRequest();
xmlHTTP.open('GET','https://jsonplaceholder.typicode.com',true);
xmlHTTP.send();
xmlHTTP.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.body.innerHTML =
this.responseText;
}
};
For server status errors, add this to onreadystatechange
if( this.status > 299 && this.readyState == 4) {
console.log('Server Error: ' + xmlHTTP.statusText);
}
For xmlHTTP code errors...
xmlHTTP.onerror = function() {
console.log('xmlHTTP Error', xmlHTTP.responseText)
}
my scriptedHTTP.js has:
var req = new XMLHttpRequest();
req.open("GET", "scripts/text.txt", true);
req.send(null);
var div = document.createElement('div');
div.innerHTML = req.responseText;
when I load the page, browser console shows:
XHR finished loading: GET "http://localhost:8000/scripts/text.txt".
but I get nothing on the page:
how do I handle responseText so it shows on the page?
thanks.
Ajax is asynchronous. req.responseText only it works when the load is performed successfully. You must use the req.onreadystatechange that is triggered when the load status has changed, so now you can use req.responseText
req.onreadystatechange = function(){
if(req.readyState == 4 && req.status == 200){
div.innerHTML = req.responseText;
}
};
Well, i'm currently developping a Google Chrome extension and i need to fetch all the DNS and 404 error to make a redirection. The problem is simply that i really don't see how it's possible...
If it's a domain error i want to fetch the domain name and for a 404 error i want to fetch the name page.
Example :
Bad Domain : http://www.justforthetest.com/ => Fetch justforthetest
404 Error : http://www.valeriemates.com/professinal.html => Fetch professinal
Hope someone could provide me some helps...
Thanks in advance !
Well, the most I was able to do is to send a new XHR request to this URL and check returned status. For wrong domains status seems to be 0, for 404 pages it is 404.
background.html
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if(changeInfo.status == "loading") {
var xhr = new XMLHttpRequest();
xhr.open("GET", tab.url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if(xhr.status == 0) {
console.log("wrong domain:", tab.url);
} else if(xhr.status == 404) {
console.log("404 page:", tab.url);
} else if(xhr.status == 200) {
console.log("regular page:", tab.url);
}
}
}
xhr.send();
}
});