Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XMLHttpRequest synchronous Issues

Status
Not open for further replies.

Nefarious1

Programmer
Feb 24, 2014
13
0
0
GB
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.

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.
 
The first thing that jumps out is that you are not loading the jquery library before trying to use it. So the first thing is to make sure it loaded. Before calling $( document ).ready(function()

Second thing, is to make sure, your server is reachable from wherever website is being loaded.

since 192.168.0.4 is an internal address, the computer that the browser that has this page opened is on, needs to have access to that internal network to be able to connect to that server. Also your port 900 is not a normal one, so I would make sure that a browser returns something when trying to open that url. ie. needs to produce relevant output when opened directly in a browser for the Ajax call to succeed.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks for the reposnse vacunita.

After some further debugging I noticed that the console reporting that the request had been blocked. I added the necessary response headers on the server and its worming now.

However, it looks like Synchronous requests are to be banned in the up-comming release of Javascript ?

Sigh,,

 
worming=working!

Cant edit your own posts on this forum ? Cant see link to do it anyway..
 
They are deprecated now, because they stop everything and wait for a response, if the response takes too long, the browser will look like its hung and be unresponsive while it waits. For that reason they are no longer going to be supported.

The Asynchronous requests let the browser continue to function and can act when they receive a response. you can usually add a small animated gif to indicate something is happening while the user waits.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top