【问题标题】:TypeError: $.ajax is not a function work fine in one page but not in anotherTypeError: $.ajax is not a function work fine in one page but not in another page
【发布时间】:2018-12-08 21:38:35
【问题描述】:

我有一个问题,当我尝试使用 ajax 函数时,我收到以下消息: TypeError: $.ajax 不是函数。

在另一个页面上的同一个项目中,AJAX 函数调用工作正常,但在此页面中它不起作用。

我在两个页面中都使用相同的标题

<!doctype html>
<html lang= "he">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
   <link rel="stylesheet" type="text/css" href="../CSS/mainCSS.css">


<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->

   <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
     <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  </head>
  <body dir ="rtl">
<button type="button" class="btn btn-primary" id = "singUpBtn">singup</button>
<button type="button" class="btn btn-primary" id = "loginBtn" >Login</button>
<script>
$('#loginBtn').click(function(){
    $.ajax({
        type:"POST",
        url:"registration.php",
        data:$('#regForm').serialize(),
        success:function(result){
                if(result == 1){
                window.location.assign("../Pages/page1.php");
                }else{
                    $('#regAlert').html(result).show();
                }
            }
        })  
})
$('#singUpBtn').click(function(){
    $.ajax({
            type:"POST",
            url:"registration.php",
            data:$('#singUpForm').serialize(),
            success:function(result){
                if(result == 1){
                window.location.assign("../Pages/page1.php");
                }else{
                    $('#singUpAlert').html(result).show();
                }
            }
        })  
})  
})
</script>
</body>
</html>

我不明白为什么在我的其他页面 page1.php 上,ajax 调用工作正常,但在我的主页上却没有。

我做错了什么?

如果有任何帮助,我将不胜感激。

【问题讨论】:

  • 控制台中何时出现错误?在页面加载时,还是在您单击按钮后?如果是在您单击按钮之后,那么这将是零意义....
  • 这是失败页面的来源吗?还是页面中有效的代码?
  • 您在所有脚本的末尾有一个额外的})。 (也缺少一堆分号......但这通常有效)我猜额外的})来自您的代码示例中缺少的document.ready。
  • 是的,额外的 }) 已准备好文档。并且代码来自不起作用的页面
  • 点击按钮后才出现错误

标签: javascript php html ajax web


【解决方案1】:

如果对您有帮助,请尝试将您的 ajax 代码替换为下面的代码

<script>
$('#loginBtn').click(function(){
  $.ajax({
    type:"POST",
    url:"registration.php",
    data:$('#regForm').serialize(),
    success:function(result){
            if(result == 1){
            window.location.assign("../Pages/page1.php");
            }else{
                $('#regAlert').html(result).show();
            }
        }
    });  
});

$('#singUpBtn').click(function(){
  $.ajax({
        type:"POST",
        url:"registration.php",
        data:$('#singUpForm').serialize(),
        success:function(result){
            if(result == 1){
            window.location.assign("../Pages/page1.php");
            }else{
                $('#singUpAlert').html(result).show();
            }
        }
    }); 
});  

</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 2020-01-18
    • 2021-11-12
    • 2015-09-18
    • 2020-03-17
    • 2013-12-22
    • 2020-01-06
    相关资源
    最近更新 更多