Nefarious1
Programmer
I am trying to send an synchronous request to the XMLHttpRequest object and monitor the response. Now I did have this code working but some some reason - it just stopped. In this example, the screen dimensions are sent to a server then the code should wait for a response.
When I try debug this code in Firefox, the response from server is always false (not 200) regardless of what response the server actually sends. I am guessing that there is a syntax error somewhere in code - but I cant see it. I have loaded it into WebStorm and cant see any errors.
I know the server is receiving the request and that a response is being sent - just seems that the function just stops working after the send command and falls over with no errors.
Any help would be much appreciated.
Thanks.
Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<button onclick="loadfunc()">Test Response</button>
<div id="get_response"> Response goes here</div>
<script>
var http;
$( document ).ready(function() {
});
function pageWidth()
{
return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}
function pageHeight()
{
return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}
function posLeft()
{
return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}
function posTop()
{
return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}
function posRight()
{
return posLeft()+pageWidth();
}
function posBottom()
{
return posTop()+pageHeight();
}
function loadfunc()
{
$height = pageHeight();
$width = pageWidth();
http = new XMLHttpRequest();
var url = "[URL unfurl="true"]http://192.168.0.4:900";[/URL]
var params = "WINPOS_HEIGHT=" + $height + "&WINPOS_WIDTH=" + $width;
http.open("GET", url+"?"+params, false);
document.getElementById("get_response").innerHTML= "sending";
http.send(null);
if (http.status == 200)
{
//alert("Ok");
document.getElementById("get_response").innerHTML = "Got answer";
}
else
{
document.getElementById("get_response").innerHTML = "Failed";
}
}
</script>
</body>
</html>
When I try debug this code in Firefox, the response from server is always false (not 200) regardless of what response the server actually sends. I am guessing that there is a syntax error somewhere in code - but I cant see it. I have loaded it into WebStorm and cant see any errors.
I know the server is receiving the request and that a response is being sent - just seems that the function just stops working after the send command and falls over with no errors.
Any help would be much appreciated.
Thanks.