Ajax POST方法错误/不工作


Ajax POST method error/not working

我试图登录我正在工作的微信页面,但我无法发布这篇特定的帖子,我不知道我做错了什么,因为我使用了很多次,错误来自内部服务器(错误500),似乎问题来自我使用的de.php,因为当我评论代码时,错误不会显示

function login() {
    var usuario = document.getElementById('user').value;
    var contra = document.getElementById('pass').value;
    $.ajax({
      type: "POST",
      url: "login.php",
      dataType: "text",
      data: { user: usuario, pass: contra }
    });
  }

php文件。。。。

$user = $_POST['user'];
$pass = $_POST['pass'];
$msg = "Error:'n";
if($user!="user"){
   $msg = $msg + "Usuario incorrecto'n"
}
if($pass!="1234"){
  $msg = $msg + "Contraseña incorrecta'n"
}
if($user=="user" && $pass=="1234"){
  header("Location: home.php");
}
echo "<script>alert(".$msg.")</script>";

在php 中使用.进行串联

$user = $_POST['user'];
$pass = $_POST['pass'];
$msg = "Error:'n";
if($user!="user"){
   $msg = $msg . "Usuario incorrecto'n";
}
if($pass!="1234"){
  $msg = $msg . "Contraseña incorrecta'n";
}
if($user=="user" && $pass=="1234"){
  header("Location: home.php");
  exit();
}
echo "<script>alert('".$msg."')</script>";

这就是PHP代码的样子:

$msg = "Error:'n";
if ( isset( $_POST['user'] ) && isset( $_POST['pass'] ) {
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    if($user!="user"){
        $msg .= "Usuario incorrecto'n";
    }
    if($pass!="1234"){
        $msg .= "Contraseña incorrecta'n";
    }
    if($user=="user" && $pass=="1234"){
        header("Location: home.php");
        exit;
    }
} else {
    $msg .= 'Please enter username and password!';
}
echo "<script>alert(".$msg.")</script>";

您需要检查$_POST变量是否设置正确。请确保文件home.php也存在!

为什么要回显脚本标记?您应该简单地回显$msg,然后在客户端上触发警报。

$.ajax({
  type: "POST",
  url: "login.php",
  dataType: "text",
  data: { user: usuario, pass: contra }
}).done(function(message) {
  alert(message);
});

);

这可能是造成这个问题的原因。我也绝对确信login.php和home.php与您的登录页面存在于同一目录中。