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); 
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效