weixin_33725272 2017-11-06 11:21 采纳率: 0%
浏览 32

未通过AJAX调用PHP文件

Below is my code for the problem. I am trying to call php file via AJAX. I have an HTML file where after form submission, the AJAX calls the PHP file. Unfortunately, I cannot see any output.

HTML File(Body):

<form>
  <label for="">Username</label>
  <input type="text" name="username" value="">
  <br><br>
  <label for="">Password</label>
  <input type="text" name="password" value="">
  <br>
  <button name="button" id="button" value="Submit">Submit</button>
</form>
<script type="text/javascript" src="jquery-3.2.1.min.js"></script>
<script src="login.js"></script>

My login.js file(for AJAX):

$("#button").click(function(e){
e.preventDefault();

$.ajax({
 type: 'POST',
 url: 'login.php'
 });
});

My PHP file:

<?php
 // Server credentials
 $serverName = "localhost";
 $username = "root";
 $password = "root";
 $dbName = "Blog";

 //Creating connection
 $db = mysqli_connect($serverName,$username,$password,$dbName);
 if(!$db){
   echo "error";
   die($db);
 }

 else{
   echo "er22ror";
 }

 mysqli_close($db);
?>
  • 写回答

2条回答 默认 最新

  • 喵-见缝插针 2017-11-06 11:25
    关注

    Your AJAX call doesn't do anything with the result:

    $.ajax({
        type: 'POST',
        url: 'login.php'
    });
    

    In order to see output, you have to output something. Use a callback for that. For example, you can log the result to the console:

    $.ajax({
        type: 'POST',
        url: 'login.php'
    }).done(function (result) {
        console.log(result);
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏