0

I'm trying to call a JS method that uses ajax and jQuery from a .js file. I have the JS method defined in a .htm file, and I have access to some, but not all of the defined methods

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/script.js">
   function testingAjax() {
      $.ajax({
         type: "GET",
         url: 'IISHander1.cs/DeleteItem',
         data: "",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (msg) {
           alert("success on all frontiers");
         },
         error: function (e) {
           alert("Something Wrong.");
         }
      });
   }
</script>

and I have another script right under that which defines:

function testingScope() { alert("in Scope");}

I've tested both and I can only call testingScope. The other results in a method not found error. Why is this?

Bahman Parsa Manesh
  • 2,314
  • 3
  • 16
  • 32
Zealia
  • 21
  • 4
  • Possible duplicate of [How do JavaScript closures work?](https://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – Endless Jul 19 '18 at 21:03
  • 2
    Your question isn't clear, you mention several other functions and files yet the code sample you posted doesn't show any of these. Please read https://stackoverflow.com/help/how-to-ask and update your question. – Adam H Jul 19 '18 at 21:03
  • Question is unclear. Are you trying to call `testingAjax` from inside `js/script.js`? If you want to do that, it has to go below. Also, I don't believe you can use both `src` *and* the body of the ` – mpen Jul 19 '18 at 21:04
  • As per the [standard](https://html.spec.whatwg.org/multipage/scripting.html#the-script-element) a ` – Lucas S. Jul 19 '18 at 21:08

1 Answers1

3

A script tag can't have both an src and code text inside it. When the src exists the textual code will be ignored

Change to

<script>
  function testingAjax() {
    $.ajax({
      type: "GET",
      url: 'IISHander1.cs/DeleteItem',
      data: "",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        alert("success on all frontiers");
      },
      error: function(e) {
        alert("Something Wrong.");
      }
    });
  }
</script>
<!-- moved below so you can call testingAjax() in this script -->
<script type="text/javascript" src="js/script.js"></script>
charlietfl
  • 170,828
  • 13
  • 121
  • 150