Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Javascript whats wrong here?

Options
  • 12-10-2009 10:28pm
    #1
    Registered Users Posts: 2,234 ✭✭✭


    Hi,
    currently learning AJAX from "Beginning javascript" (WROX).The code here is adirect copy from the book.

    I keep getting the error "oHttp is undefined" in my browser.

    Function to create xmlHttpRequest:
    // Create and return an XmlHttpRequest object.
    function createXmlHttpRequest()
    {
    	// XMLHttpRequest exists
    	// i.e. Client is IE7+,FF,Opera,Safari etc..
    	if(window.XMLHttpRequest)
    	{
    		var oHtttp = new XMLHttpRequest();
    		document.write('Client is not IE5 or IE6');
    		return oHttp;
    	}
    	// Else client is IE5 or IE6
    	else if( window.ActiveXObject)
    	{
    		var versions = ["MSXML2.XmlHttp.6.0","MSXML2.XmlHttp.3.0"];
    		
    		// Try to create a XMLHttpRequest with the latest version.
    		for(var i=0;i<versions.length; i++)
    		{
    			try
    			{
    				var oHttp = new activeXObject(versions[i]);
    				document.write('Client is IE5 or IE6');
    				return oHttp;
    			}
    			catch (error)
    			{
    				// Do nothing here
    			}
    		}
    	}
    
    	// If all of the above fails then the client is not AJAX compatible.
    	return null;
    }
    

    From paeg body:
    <script type="text/javascript">
    	var oHttp = createXmlHttpRequest();
    	
    	oHttp.open("GET", "http://localhost/ajax/test.txt", false);
    	oHttp.send(null);
    
    	if(oHttp.status == 200)
    	{
    		alert('File found!');
    	}
    	else if(oHttp.status == 404)
    	{
    		alert('File not found!');
    	}
    	else
    	{
    		alert('An error occured while attempting to retrieve the file');
    	}
    </script>
    

    What am I missing here..?

    Thanks.

    P.S. I feel silly now:(


Comments

  • Moderators, Politics Moderators Posts: 39,848 Mod ✭✭✭✭Seth Brundle


    Is the error in IE or FF or both?


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Both,

    In IE I get "oHttp is null or not an object".


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Problem is your declaration has 3 t's.

    "var oHtttp = new XMLHttpRequest();"

    You need to either remove a T, or make reference to the oHtttp object.


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Oops that seems to be a typo.

    I have the spelling correct in my code. Also the new should be omitted. The line should be:
    var oHttp = createXmlHttpRequest();
    


  • Moderators, Politics Moderators Posts: 39,848 Mod ✭✭✭✭Seth Brundle


    good spot dlofnep!


  • Advertisement
  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Yeah possibly, I'm not familiar with Ajax, just Java. I know when you are creating a new instance of an object, you need to use the new keyboard, maybe not here if you say so :)

    In anycase, yeah - it was just the typo that was the issue. Anytime you see errors regarding an object being undefined - just check the declaration :)


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    kbannon wrote: »
    good spot dlofnep!

    Thanks ;)


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Oops, I think I was a little unclear about the error. For some reason the code in my script is fine but the code on here is wrong..

    ..So, I still have the problem. With or without the new keyword. With it I get a different error:

    "oHttp.open is not a function"


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Can you post the updated code?


  • Registered Users Posts: 2,234 ✭✭✭techguy


    dlofnep wrote: »
    Can you post the updated code?

    No problem.

    library.js:
    // Create and return an XmlHttpRequest object.
    function createXmlHttpRequest()
    {
    	// XMLHttpRequest exists
    	// i.e. Client is IE7+,FF,Opera,Safari etc..
    	if(window.XMLHttpRequest)
    	{
    		var oHttp = new XMLHttpRequest();
    		document.write('Client is not IE5 or IE6');
    		return oHttp;
    	}
    	// Else client is IE5 or IE6
    	else if( window.ActiveXObject)
    	{
    		var versions = ["MSXML2.XmlHttp.6.0","MSXML2.XmlHttp.3.0"];
    		
    		// Try to create a XMLHttpRequest with the latest version.
    		for(var i=0;i<versions.length; i++)
    		{
    			try
    			{
    				var oHttp = new activeXObject(versions[i]);
    				document.write('Client is IE5 or IE6');
    				return oHttp;
    			}
    			catch (error)
    			{
    				// Do nothing here
    			}
    		}
    	}
    
    	// If all of the above fails then the client is not AJAX compatible.
    	return null;
    }
    
    

    exerpt from html body:
    <script type="text/javascript">
    	var oHttp = createXmlHttpRequest();
    	
    	oHttp.open("GET", "http://www.google.com", false);
    	try
    	{
    		oHttp.send(null);
    	}
    	catch  (error)
    	{
    		// do nothing.
    	}
    alert(oHttp.status);
    	if(oHttp.status == 200)
    	{
    		alert('File found!');
    	}
    	else if(oHttp.status == 404)
    	{
    		alert('File not found!');
    	}
    	else
    	{
    		alert('An error occured while attempting to retrieve the file');
    	}
    </script>
    

    Once the error with the three t's was fixed I got an uncaught exception error. I needed to put the oHttp.send(null) line in a try catch block.
    The runs through fine now displaying the status etc.

    Thanks for the help guys..


  • Advertisement
  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Congrats :)


Advertisement