Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    JUD
    JUD is offline
    New Coder
    Join Date
    Jul 2005
    Location
    I'm right here
    Posts
    92
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Ajax function works in IE7, Google Chrome & Safari but not in FF3

    I've come up with this script to return responseText or responseXML depending on which function I call.

    Code:
    AJAX = {
    	req: false,
    	res: '',
    	
    	requireText: function(url){
    		if(window.XMLHttpRequest){
    			req = new XMLHttpRequest;	
    		}else if(window.ActiveXObject){
    			try{
    				req = new ActiveXObject("MSXML2.XMLHTTP");	
    			}catch(e){
    				try{
    					req = new ActiveXObject("Microsoft.XMLHTTP");	
    				}catch(e){}
    			}
    		}else{
    			alert("Your browser doesn't support AJAX");
    			return;
    		}
    		
    		if(req){
    			req.onreadystatechange = function(){
    				if(req.readyState == 4 && req.status == 200){
    					res = req.responseText;	
    				}
    			}
    			req.open("GET", url, false);
    			req.send(null);
    		}
    		
    		return res;
    	},
    	
    	requireXml: function(url){
    		if(window.XMLHttpRequest){
    			req = new XMLHttpRequest;	
    		}else if(window.ActiveXObject){
    			try{
    				req = new ActiveXObject("MSXML2.XMLHTTP");	
    			}catch(e){
    				try{
    					req = new ActiveXObject("Microsoft.XMLHTTP");	
    				}catch(e){}
    			}
    		}else{
    			alert("Your browser doesn't support AJAX");
    			return;
    		}
    		
    		if(req){
    			req.onreadystatechange = function(){
    				if(req.readyState == 4 && req.status == 200){
    					res = req.responseXML;	
    				}
    			}
    			req.open("GET", url, false);
    			req.send(null);
    		}
    		
    		return res;
    	}
    }
    I call the functions like this:

    Code:
    var textRes = AJAX.requireText("path/to/myFile");
    and

    Code:
    var xmlRes = AJAX.requireXml("path/to/myFile");
    Everything is working fine in IE7, Chrome & Safari but FF3(FireBug) gives me an error:

    Code:
    res is not defined
    I've been trying different things for hours but I can't get it to work.

    Any help would be greatly appreciated.

  • #2
    JUD
    JUD is offline
    New Coder
    Join Date
    Jul 2005
    Location
    I'm right here
    Posts
    92
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Never mind. After searching Google for a bit I've finally figured it out.

    Turns out I was using the onreadystatechange handler when I didn't need to be as I'm opening the XmlHttpRequest in synchronous mode.

    Everythings working fine now.


  •  

    Posting Permissions

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