2

我在向登录脚本发送 ajax 请求的 js 函数上收到此错误。这是代码:

function validateLogin(){
  $(document).ready(function(){
    $("#loginform").submit(function(){
      $.ajax({
        type: "POST",
        url: "./php/autologin.php",
        data: {
          'login': $("#login").val(), 
          'password': $("#password").val()
        },
        dataType: "json",
        success:function(data){
          if(data.status === "success"){
            alert(data.message);
            window.location = "./userarea.html?user=".concat(data.user);
          } else if (data.status === "error"){
            alert(data.message);
          }
        },
        error:function(thrownError){
          console.log(thrownError);
        }
      }); 
      return false;
    });

  });
}

我在上面的代码中的 url 上收到 POST 500 Internal Server Error,这是 chrome 控制台中打印的内容:

POST http://localhost/capstone/atomos-4.0/php/autologin.php 500 (Internal Server Error) jquery-1.7.2.min.js:4
f.support.ajax.f.ajaxTransport.send jquery-1.7.2.min.js:4
f.extend.ajax jquery-1.7.2.min.js:4
(anonymous function) validateLogin.js:4
f.event.dispatch jquery-1.7.2.min.js:3
f.event.add.h.handle.i jquery-1.7.2.min.js:3

我真的不明白发布错误试图告诉我什么,任何人都可以在正确的方向上推动我解决这个问题。如果您需要更多信息,请告诉我。谢谢

编辑:这是有问题的 php 函数: autologin.php

include './login.php';

$login = $_POST['login'];
$password = $_POST['password'];
$loginattempt = json_decode(login($login, $password));

if ($loginattempt->{'status'} === "success") {
  return json_encode(array('status' => "success", 'message' => "Login Successful!"));
  die();
} else { 
  return json_encode(array('status' => "error", 'error' => "loginfailure", 'message' => ($loginattempt -> {"message"})));
  die();
}
?>

login.php

<?php

include './connnect_to_mysql.php';

function login($log, $pass) {
  $connection = json_decode(connect_to_mysql());
  if ($connection->{'status'} === "success") {
    $sqlquery = mysql_query("SELECT * FROM userdata WHERE login='$log' AND password='$pass'") or die(mysql_error());
    if (mysql_num_rows($sqlquery) == 1) {
      setcookie("user", $log, 60 * 60 * 24);
      return json_encode(array('status' => "success", 'message' => "Login Successful.", 'user' => $log));
      die();
    } else {
      return json_encode(array('status' => "error", 'error' => "loginfailure", 'message' => mysql_error()));
      die();
    }
  } else {
    return json_encode(array('status' => "error", 'error' => "connectionerror", 'message' => $connection -> {'message'}));
    die();
  }
}

?>

编辑2 connect_to_mysql.php::

<?php

function connect_to_mysql() {
  $con = mysql_connect('localhost', 'user', 'pass') or die(mysql_error());
  if ($con) {
    $db = mysqli_select_db($con, 'db')or die(mysql_error());
    if ($db) {
      return json_encode(array('status' => "success", 'message' => "connected"));
      die();
    }
  } else {
    return json_encode(array('status' => "error", 'message' => mysql_error()));
    die();
  }
}

?>
4

1 回答 1

1

修复include './connnect_to_mysql.php';include './connect_to_mysql.php';:)

于 2012-06-15T02:22:59.590 回答