[Need Help] Ajax problem: Works in IE6 and not in Firefox

Discussion in 'JavaScript' started by testfrozen, Mar 9, 2010.

  1. #1
    Even I am a fan of FF, but this is strange problem I am running through.

    I am new to ajax and learning to work on it based on what I read online. Here is the code which I wrote which simply fetches the web page and displays on my page.

    It works well with IE6, but not on FF. I have not tested with IE7 or 8.

    Kindly have a look at the code below and suggest me changes to make it work on the best browser.

    
    <html>
    <head>
    <script type="text/javascript">
    	function testgoogle(){
    		if (window.XMLHttpRequest)
    		{// code for IE7+, Firefox, Chrome, Opera, Safari
    			alert('ff');
    			xmlhttp=new XMLHttpRequest();
    		}
    		else
    		{// code for IE6, IE5
    			alert('old ie');
    			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		xmlhttp.open('get', 'https://www.paypal.com/', true);
    		xmlhttp.onreadystatechange=function()
    		{
    			if(xmlhttp.readyState==4)
    			  {document.getElementById('test').innerHTML=xmlhttp.responseText}
    		}
    		
    		xmlhttp.send(null);
    	}
    </script>
    </head>
    <body>
    	<input value="hello" type="button" onclick="testgoogle(null);" />
    	<div id="test"></div>
    </body>
    </html>
    
    HTML:
    Thanks
    TF
     
    testfrozen, Mar 9, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Try this code and you'll see what is the problem:

    
    <html>
    <head>
    <script type="text/javascript">
        function testgoogle(){
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                alert('ff');
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                alert('old ie');
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            try{
              xmlhttp.open('get', 'https://www.paypal.com/', true);  
            }catch(e){
              alert(e);
            }
            xmlhttp.onreadystatechange=function()
            {
                if(xmlhttp.readyState==4)
                  {document.getElementById('test').innerHTML=xmlhttp.responseText}
            }
    
            xmlhttp.send(null);
        }
    </script>
    </head>
    <body>
        <input value="hello" type="button" onclick="testgoogle(null);" />
        <div id="test"></div>
    </body>
    </html>
    
    Code (markup):
     
    s_ruben, Mar 10, 2010 IP
  3. testfrozen

    testfrozen Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your help, but even it did not work. A few minutes back I read that FF does not support cross domain ajax requests as a security feature. Now, I am looking for the methods for that. If you know any method for cross domain requests, kindly inform.

    MS has recently introduced such feature in IE8 called XDomainRequest. Check it out here: http:// msdn.microsoft.com/en-us/library/cc288060%28VS.85%29.aspx

    Thanks
    TF
     
    testfrozen, Mar 10, 2010 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    its not a big drama. you need a proxy. i wrote an example using yahoo's YQL a while back:
    http://forums.digitalpoint.com/showthread.php?t=1649430&highlight=

    I just posted this on my blog as it seems like a sore subject for many - http://fragged.org/cross-domain-ajax-calls-via-yql-as-proxy-and-mootools-jsonp_1028.html

    this uses mootools as framework but there are plenty of ways of doing this via jquery or vanilla js even, if you set your mind to it. do not use xdomainrequest, proprietary and unsupported things are a bad idea
    regards
     
    Last edited: Mar 10, 2010
    dimitar christoff, Mar 10, 2010 IP
  5. testfrozen

    testfrozen Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks Dimitar :) I hope to learn more to use your way of implementation.
     
    testfrozen, Mar 10, 2010 IP