1

this is my 1st javascript attempt and I'm getting the following in the chrome console:

"Uncaught ReferenceError: test1 is not defined."

I'm trying to send an Ajax request via onclick button event and display the data in the document. However, due to the error, I've scoped back to hello-world just for a debug attempt. Pardon this kind of question as I know its very elementary.

Any help is appreciated. Thanks for your time!

<!DOCTYPE html>
<html>
<head>
</head> 
<body>

<button onclick="test1()">Click Here</button>
<p id="Data"></p>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">

function test1(){
alert("Hello, World");
/* $.ajax({
    url: Target_URL,
    headers: {
        'Accept':'application/json',
        'authorization', 'Bearer ' + vToken
    },
    method: 'GET'
   // dataType: 'json',
   // data: YourData,
    success: ajaxCall = data
  }); */
}
</script>

</body>

</html>
Arch1medes
  • 39
  • 1
  • 8

5 Answers5

7

You used only one script tag for loading jquery and for your method - that will not work... "If a script element has a src attribute specified, it should not have a script embedded inside its tags." here a working solution:

<!DOCTYPE html>
<html>
<head>
</head> 
<body>

<button onclick="test1()">Click Here</button>
<p id="Data"></p>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
function test1(){
alert("Hello, World");
/* $.ajax({
    url: Target_URL,
    headers: {
        'Accept':'application/json',
        'authorization', 'Bearer ' + vToken
    },
    method: 'GET'
   // dataType: 'json',
   // data: YourData,
    success: ajaxCall = data
  }); */
}
</script>

</body>

</html>
Meiko Rachimow
  • 4,664
  • 2
  • 25
  • 43
1

You can't place your javascript code in same <script> tag where you linked an external file (it shouldn't be included in <script src="..."> function </script>). Your test1 function should be separated from scr javascript tag. Try this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">/<script>
<script> your javascript function should be here </script>

Hope this will solve your problem.

statosdotcom
  • 3,109
  • 2
  • 17
  • 40
-1

Put the JavaScript in the head of your document, and separate out the script for including jquery and your actual code.

<!DOCTYPE html>
<html>
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 

<script>
function test1(){
    alert("Hello, World");
}
</script>
</head> 
<body>
    <button onclick="test1()">Click Here</button>
    <p id="Data"></p>
</body>
</html>
Stuart
  • 6,630
  • 2
  • 24
  • 40
-1

You haven't placed your javascript method in proper javascript tag, separately place javascript method and javascript src, please check below for more clarification:

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<script type="text/javascript">
  function test1() {
    alert("Hello, World");
  }
/* $.ajax({
    url: Target_URL,
    headers: {
        'Accept':'application/json',
        'authorization', 'Bearer ' + vToken
    },
    method: 'GET'
   // dataType: 'json',
   // data: YourData,
    success: ajaxCall = data
  }); */
</script>

</head> 
<body>

<button onclick="test1()">Click Here</button>
<p id="Data"></p>

</body>

</html>
krishna Prasad
  • 3,541
  • 1
  • 34
  • 44
-3

First off, you need to declare your JavaScript function before you call it.

Change to this:

<!DOCTYPE html>
<html>
<head>
</head> 
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js" />
<script>
    function test1(){
        alert("Hello, World");
    }
</script>

<button onClick="test1()">Click Here</button>
<p id="Data"></p>

</body>

</html>
st_443
  • 51
  • 1
  • 6
  • The function is defined just fine. The problem is trying to load an external script and writing code inside the same script tag. – Ryan Dantzler Mar 29 '16 at 23:52