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();
}
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
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)
}
Everytme when i call ajax and i use timeout to repeat the ajax function, after a few minutes, i can't have access to my website, it;s like my IP address gets blocked from my server, by the way i am using 000webhost for hosting my server. The code is below .Can someone assist me and tell me what i can do solve this problem without telling me to use websockets/comets, i want to use AJAX for very frequent update maybe up to 10 seconds. Not a most but it will be great if someone can show solutions without the use use of JQuery Thank you.
function display_message(user,id){
var xhr;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE 8 and older
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
/**********************************************************/
var data = "user="+user+"&id="+id;
xhr.open("POST", "message_read.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
xhr.onreadystatechange = display_data;
function display_data() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
document.getElementById("message_box").innerHTML =xhr.responseText;
mymessage =setTimeout(display_message(user,id), 5000);
} else {
alert('There was a problem with the request.');
}
}
}
}
I'm not very familiar with APIs in general, but don't let that discourage you from answering this question ;)
I'm trying to pull data using the XMLHTTPRequest object. I was originally going to use a WSDL file, but the API I'm using does not have one. For some reason, when I'm trying to get the response displayed in an HTML file, nothing comes across, not even the error that it didn't connect. The api also requires the use of POST.
Here's the javascript:
window.onload = function startCall() {
var http = new XMLHttpRequest();
var url = "https://api.domain.com";
var params = "Version=2.00&ApiKey=111111111111111111&CallID=001";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", "226");
http.setRequestHeader("Host", "api.domain.com");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
function sendToHtml(url, 'target') {
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById('target').innerHTML = req.responseText;
}
}
else {
document.getElementById('target').innerHTML=" Error:\n"+ req.status + "\n" +req.statusText;
}
}
}
And here's the HTML
<html>
<head>
<script type="text/javascript" src="call.js"></script>
</head>
<body>
<span onload="startCall" id="target">
</span>
</body>
</html>
Thankyou for any help
POST requests between domains are restricted so browser will not allow it.
There is option to enable it (CORS) by sending correct Access-Control-Allow-Origin, but it have to be done on API server.
We are running windows IIS 6 and use it's native ability to protect files with Windows Authentication as our login method.
It works fine, except that when I try to post method XMLHttpRequest from IE i get the login dialog again, which causes the request to fail.
The weird thing is that Mozilla and Safari work well.
Is there something I can do with the headers or something to make IIS recognize it as the same session, and not promt a re-login?
function ajaxQuery(method, url, params, asynchronous, readyFunction, is_done) {
if (asynchronous == null) {
asynchronous = true;
}
//alert("URL: "+url);
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
readyFunction(xmlhttp.responseText);
if (is_done) {
is_done("ok");
}
}
}
if (method.toLowerCase() == "get") {
url += "?" + params;
params = null;
}
debug = url;
xmlhttp.open(method, url, asynchronous);
if (method.toLowerCase() == "post") {
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
debug = params.length;
}
xmlhttp.send(params);
}
Check the response headers coming back. We had the same problem with an app that was (intentionally) returning a 401 code. IIS caught that code and added the WWW-Authenticate header which forces IE to display the login. We were able to fix it by returning 403 instead.