ajax方法的范围有何不同?

问题描述 投票:0回答:1

我正在尝试调用一个使用.js文件中的ajax和jQuery的JS方法。我有一个.htm文件中定义的JS方法,我可以访问一些但不是所有已定义的方法

<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>

我在另一个脚本下面定义了:

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

我已经测试了两个,我只能调用testingScope。另一个导致找不到错误的方法。为什么是这样?

javascript jquery ajax scope
1个回答
3
投票

脚本标记不能同时包含src和代码文本。当src存在时,文本代码将被忽略

改成

<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>
© www.soinside.com 2019 - 2024. All rights reserved.