weixin_33721344 2017-08-02 08:45 采纳率: 0%
浏览 50

Ajax无法运行php文件

I used similar code like below to show database tables. It was working then but now it's not working. I have tried many other codes but my post file is not called no matter what I do.

<div class="loginform-in">
  <h1>User Login</h1>
  <div class="err" id="add_err"></div>
  <fieldset>
    <form action="./" method="post">
      <ul>
        <li>
          <label for="name">Username </label>
          <input type="text" size="30"  name="name" id="name" />
        </li>
        <li> 
          <label for="name">Password</label>
          <input type="password" size="30"  name="word" id="word" />
        </li>
        <li> 
          <label></label>
          <button id="login" type="button" class="btn btn-default" onclick="saveBasics();">Login</button>
      </ul>
    </form> 
  </fieldset>
</div>

<script src="jquery-2.2.3.min.js"></script>
<script src="login.js"></script>

login.js

alert("hi");

$("#login").click(function(){   
  username = $("#name").val();
  password = $("#word").val();

  $.ajax({
    url: "login.php",
    type: "POST",
    data: "name=" + username + "&pwd=" + password,
    success: function(html) { 
      aler(html);
      if (html == 'true') {
        window.location = "login.php";
      } else {
        $("#add_err").css('display', 'inline', 'important');
        $("#add_err").html("<img src='images/alert.png' />Wrong username or 
password");
      }
    },
    beforeSend: function() {
      $("#add_err").css('display', 'inline', 'important');
      $("#add_err").html("<img src='images/ajax-loader.gif' /> Loading...")
    }
  });
  return false;
});

login.php

<?php
  echo '<script language="javascript">';
  echo 'alert("message successfully sent")';
  echo '</script>';
?>

My code is not running login.php file and I don't know why. It should show alert when button is clicked but it doesn't.

Note: I just added alert(html); in my java script file and the alert shows

    <script language="javascript">alert("message successfully sent")
    </script>true

this

Edited: Every body The javascript is working fine (I think) its just that the response I am getting is the literal php page code.

  • 写回答

4条回答 默认 最新

  • weixin_33692284 2017-08-02 08:55
    关注

    Open browsers console (F12) and see what errors are you getting?

    Also, there is a Network tab.

    If you will filter the XHR in the Network tab - then you will see, if your request is actually addressing the login.php page and you can see what the page has answered (the Response sub-tab).

    Usually, I use more convenient way to fire ajax requests:

    $.post('login.php', {
        name: $('#name').val(),
        pwd: $('#word').val(),
    }, function(response) {
        console.log(response);
        alert(response); 
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料