www.webdeveloper.com
Results 1 to 5 of 5

Thread: XMLHttpRequest not getting results back

Hybrid View

  1. #1
    Join Date
    Dec 2009
    Posts
    15

    XMLHttpRequest not getting results back

    I'll make it short. Here's the code (the GET request is taken from a Google example):
    Code:
    <html>
    	<script>
    		function hello() {
    			var xmlhttp = null;
    			if (window.XMLHttpRequest) {
    				xmlhttp = new XMLHttpRequest();
    				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", "http://www.google.com/search?hl=en&q=benjamin+schirmer", true);
    			xmlhttp.send();
    					
    			xmlhttp.onreadystatechange = function(){
    				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    					alert("OK");
    				}
    				else {
    				}
    			};
    			
    		}
    	</script>
    	<body onload="hello()"> 
    	</body>
    </html>
    The problem: the alert never displays. Firebug shows xmlhttp.status = 0 and responseText = "" all the time. The page is on my local machine.

    Any ideas on how to get this to work? Thanks!

    P.S> I've tried doing a synchronous call as well but that gives me an error message; something like 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    I copy and pasted your code and it works fine in IE8 but not in FF3.6 or OP10.

    In IE8 I get back the html from the google search results page.

    I modified and added an alert (see below) to make sure the ajax response is coming back.

    I suspect that maybe the way google is outputing its code is causing problems for FF but not IE. AJAX nirmally sends back either xml data or the output from echo statements in php or the equivalent in other server side scripting languages.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <
    title>Untitled Document</title>

    <
    script>
      function 
    hello() {
       var 
    xmlhttp null;
       if (
    window.XMLHttpRequest) {
        
    xmlhttp = new XMLHttpRequest();
        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""http://www.google.com/search?hl=en&q=benjamin+schirmer"true);
       
    xmlhttp.send();
         
       
    xmlhttp.onreadystatechange = function(){
                
                    
    alert(xmlhttp.readyState);
                
        if (
    xmlhttp.readyState == && xmlhttp.status == 200) {

         
    alert(xmlhttp.responseText);
        }

        else {
        }
       };
       
      }
     
    </script>
     <body onload="hello()"> 
    </html> 

  3. #3
    Join Date
    Dec 2009
    Posts
    15
    That's an interesting find. I was only testing it on Firefox and Chrome. So I've decided to look at another alternative -and the first thing I thought of was dojo. But that one is not working either...

    Web dev can be a very interesting job. NOT!

  4. #4
    Join Date
    Mar 2010
    Posts
    2,803
    Quote Originally Posted by OpelCorsa View Post
    That's an interesting find. I was only testing it on Firefox and Chrome. So I've decided to look at another alternative -and the first thing I thought of was dojo. But that one is not working either...

    Web dev can be a very interesting job. NOT!
    Maybe it's character building

  5. #5
    Join Date
    Dec 2009
    Posts
    15
    Ok, so back to the original post. I'd heard that you cannot make GET requests to servers that are located in a different domain. But that code worked for Google. What the hell?! Am I having a case of Friday?

    Please help defrost my brain -it's beginning to hurt.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles