PHP脚本上的jQuery.ajax POST 500内部服务器错误

时间:2012-06-15 02:16:36

标签: php jquery ajax internal-server-error

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

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

  });
}

我从上面的代码中获取了网址上的POST 500内部服务器错误 这是在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();
  }
}

?>

1 个答案:

答案 0 :(得分:1)

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