INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

XMLHttpRequest synchronous Issues

XMLHttpRequest synchronous Issues

(OP)

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 = "http://192.168.0.4:900";
      
    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.

RE: XMLHttpRequest synchronous Issues

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. http://192.168.0.4:900 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

RE: XMLHttpRequest synchronous Issues

(OP)
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,,

RE: XMLHttpRequest synchronous Issues

(OP)
worming=working!

Cant edit your own posts on this forum ? Cant see link to do it anyway..

RE: XMLHttpRequest synchronous Issues

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close