0

I'm just trying to execute an Ajax request but instead end up having a ReferenceError: getData is not defined on Console. And this piece of code worked fine 6 months back or so. I've already referred some previously asked questions but no good.

Here's the code:

<html>
<head>
<script language = "text/javascript"> 
  var XMLHttpRequestObject = false; 
       if (window.XMLHttpRequest) { 
   XMLHttpRequestObject = new XMLHttpRequest(); 
 } else if (window.ActiveXObject) { 
   XMLHttpRequestObject = new  
     ActiveXObject("Microsoft.XMLHTTP"); 
 } 
 function getData(dataSource, divID)  
 {  
   if(XMLHttpRequestObject) { 
     var obj = document.getElementById(divID);  
     XMLHttpRequestObject.open("GET", dataSource);  
     XMLHttpRequestObject.onreadystatechange = function()  
     {  
       if (XMLHttpRequestObject.readyState == 4 &&  
         XMLHttpRequestObject.status == 200) {  
           obj.innerHTML = XMLHttpRequestObject.responseText;  
       }  
     }  
     XMLHttpRequestObject.send(null);  
   } 
 } 
</script> 
</head>
<body>

<form> 
  <input type = "button" value = "Fetch the message"  
    onclick = "getData('data.txt', 'targetDiv')">  
</form> 

<div id="targetDiv"> 
  <p>The fetched message will appear here.</p>  
</div>  

</body>
</html>
8
  • 1
    What browser are you using and what is your full error message (with call stack if provided)? Commented Jun 3, 2014 at 17:59
  • Chrome : Uncaught ReferenceError: getData is not defined and firefox : Exception : getData is not defined Commented Jun 3, 2014 at 18:02
  • @SurrealDreams Not everybody likes jQuery, that's not what the OP is asking about... Commented Jun 3, 2014 at 18:03
  • it's not what Javascript can't do, it's for learning purpose. Why isn't this working...? Commented Jun 3, 2014 at 18:04
  • @raaaz You're not showing us everything. If I paste your code onto jsfiddle.net/mendesjuan/933VJ, it does not throw the error you mentioned Commented Jun 3, 2014 at 18:05

1 Answer 1

3

Your browser doesn't know what language "text/javascript" is. It only knows the language "javascript" and the MIME-type "text/javascript". You're mixing the two up.

You could either change it to language="javascript" or type="text/javascript".

<script type="text/javascript">

language is an outdated attribute anyway; type is more modern.

Sign up to request clarification or add additional context in comments.

2 Comments

It is indeed the problem... jsfiddle.net/mendesjuan/933VJ/1 Modern browsers assume type="text/javascript" so you can also leave it out jsfiddle.net/mendesjuan/933VJ/3
And we have a winner here! Nice observation, Nathan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.