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)
}
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 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.
We are three developers working on a web-app project with Django.
Some communications client-server use an API. One particular request is not sending on one of the developer's computer. We use the same Browser (Firefox quantum 64.0 (64 bits)), same OS (Ubuntu 18.04).
Here is the JS snippet:
function requestHandler(url){
let xhttp = new XMLHttpRequest();
xhttp.open('POST', url, true);
xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhttp.setRequestHeader('X-CSRFToken', getCookie('csrftoken'));
xhttp.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200) {
drawLogTable();
}
else if (this.readyState == 4){
console.log(this);
new PNotify({
title: 'Échec!',
text: "Opération impossible: " + this.statusText,
type: 'error'
});
}
}
xhttp.send();
}
Note that url is '../../api/log?action=accept&id=1'
On all the working computers and browsers, the request posts and does a 404 since server-side has no implementation yet. On the non-working computer, chromium works, but under Firefox, the request is not even sent (server log is empty) and the request object is completely empty (no status, no text).
We have other AJAX requests in the same type that are working fine.
Can you try this:
xhttp.onload = function(){
if (this.status == 200) {
drawLogTable();
}
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?
Am trying to get http response from php web service in javascript, but getting null in firefox and chrome. plz tell me where am doing mistake here is my code,
function fetch_details()
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest()
alert("first");
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP")
alert("sec");
}
xhttp.open("GET","url.com",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;
alert(xmlDoc.getElementsByTagName("Inbox")[0].childNodes[0].nodeValue);
}
I have tried with ajax also but am not getting http response here is my code, please guide me
var xmlhttp = null;
var url = "url.com";
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
alert(xmlhttp); //make sure that Browser supports overrideMimeType
if ( typeof xmlhttp.overrideMimeType != 'undefined')
{
xmlhttp.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert('Perhaps your browser does not support xmlhttprequests?');
}
xmlhttp.open('GET', url, true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4)
{
alert(xmlhttp.responseXML);
}
};
}
// Make the actual request
xmlhttp.send(null);
I am getting xmlhttp.readyState = 4 xmlhttp.status = 0 xmlhttp.responseText = ""
plz tell me where am doing mistake
Your doing a cross domain request.
You are only allowed to do xmlhttp requests to the same host.
I can't read any of that but Chrome has a JavaScript console that will probably tell you what you're doing wrong.
It's a cross-domain issue, to resolve it the server response header should contain "access-control-allow-origin"
If your server is coded in PHP, the header should be like the following example:
<?php
header('Content-type: text/html');
header('Access-Control-Allow-Origin: *');
$uri = 'http'. ($_SERVER['HTTPS'] ? 's' : null) .'://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo('<p>This information has come from ' . $uri . '</p>');
?>