weixin_33725126 2015-02-04 12:50 采纳率: 0%
浏览 26

Ajax调用根本不起作用[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming" dir="ltr">What is the difference between client-side and server-side programming?</a>
                            <span class="question-originals-answer-count">
                                (4 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2015-02-04 22:50:51Z" class="relativetime">5 years ago</span>.</div>
        </div>
    </aside>

i have a very simple ajax call, just so i can test first:

<script  src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
function validateLogIn()
{
  var username  = $("#username").val();     
  console.log(username,password,login,remember);
  //alert('Given data is not correct' + username + "   " + password);
  $.ajax({     
    url: 'validate.html'                         
    type: 'POST', // POST/GET
    data: { 'username' : username } //for now just pass username
    }).done(function(response){  //Attach a succes handler
    alert(response); //my complete code in validate.html
                     // displays in this alert!
    }); 
  }); 
}

 </head>
 <body>
   <form action="crud.html" method="post" name="form_submit" onsubmit="return validateLogIn()">
      <input required placeholder="Username" type="text" name="username" id="username"/>
      <input required placeholder="Password" type="password" name="password" id="password"/>
      <label for="remember">Remember Me:</label>
    <input type="checkbox" name="remember" value="yes" id="remember" />
    <br />
    <br />
    <input type="submit" name="login" value="login" id="login"/>
</form>

in validate.html: none of this gets executed

 <?php
  echo htmlspecialchars($_POST['username'], ENT_QUOTES, 'UTF-8');
  echo $username = $_POST['username']; 
  <script type='text/javascript'>alert('$username');</script>
</div>
  • 写回答

1条回答 默认 最新

  • weixin_33713503 2015-02-04 12:54
    关注

    I don't think you understand AJAX at all. I've made an example below very basic with comments.

    var username  = $("#username").val();
    
    $.ajax({     
        url: 'validate.php',  // << You can't have php in html files. 
        type: 'POST', // POST/GET
        data: { 'username' : username } //Data to be send will be in $_POST
      }).done(function(response){  //Attach a succes handler
         alert(response); //Alert the response from validate.php
      }); 
    }
    

    PHP FILE:

    <?php
    echo htmlspecialchars($_POST['username'], ENT_QUOTES, 'UTF-8'); //XSS PREVENTION
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果