javascript Jquery、php ajax post 500 内部服务器错误返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15928340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:38:36  来源:igfitidea点击:

Jquery, php ajax post 500 Internal Server Error returns

phpjavascriptajaxjquery

提问by Gábor

I have some problem with jquery ajax & php. I'm not good in php, but I have to create a login page with php.

我对 jquery ajax 和 php 有一些问题。我不擅长 php,但我必须用 php 创建一个登录页面。

The promblem is, when I try to get some information from the server I get server error insted of the value.

问题是,当我尝试从服务器获取一些信息时,我得到了值的服务器错误。

Here is my php code (if someone know better method to create login on server side, please correct me):

这是我的 php 代码(如果有人知道在服务器端创建登录的更好方法,请纠正我):

<?php
//$link = mysql_connect('localhost', 'root', '') or die('A kapcsolódás nem sikerült: '+mysql_error());
$link = mysql_connect("127.0.0.1", "qm", "soR624gA") or die("Nem sikerült kapcsolódni az adatbázishoz!"+mysql_error()); 
mysql_query("SET NAMES UTF8");
mysql_select_db("qm", $link);
$uname = mysql_real_escape_string($_POST['name']);
$pass = mysql_real_escape_string($_POST['pass']);
$fetch = mysql_query("SELECT * FROM felhasznalok WHERE nev ='{$uname}' AND jelszo = Sha1('{$pass}')") or die(mysql_error());
if (mysql_num_rows($fetch) == 1) {
    $a = array();
    $['true'] = 'true';
    echo json_encode($a);
}else{
    $a = array();
    $['true'] = 'true';
    echo json_encode($a);
}
?>

And here is the code of the login on the client side:

这是客户端登录的代码:

function handleLogin(){
var _url = preUrl + "login.php";
var name = $("#loginForm #name").val();
var pass = $("#loginForm #pass").val();

$.ajax({
    type: 'POST',
    url: _url,
    data: {name: name, pass: pass},
    dataType: 'jsonp',
    beforeSend: function(){
        if((!name) || (!pass)){
            notify('error','Kérlek t?lts ki minden adatot!');
            return false;
        }else{
            $("#loginForm #loginBtn").prop("disabled", true);  
        }
    },
    success: function(data){
    if(data[0].true == 'true'){
            window.localStorage["username"] = name;
            window.localStorage["password"] = pass;  
            $.mobile.changePage("#wall",{transition:"slide", reverse:false});
        }else{
            $('#loginForm #name').val("");
            $('#loginForm #pass').val("");
            notify('error','Hibás adatok!');
        }
    },
    error: function(err){
    //átírni ha a cordovajs be lesz szúrva!!!
    alert('Hiba: '+err.message);
}
});

$("#loginForm #loginBtn").prop("disabled", false);
return false;
}

I tried it out on different servers but nothing changed. I only get the error.

我在不同的服务器上尝试过,但没有任何改变。我只得到错误。

回答by Marc B

learn basic php: $['true'] = 'true';is a flat-out syntax error. this is the most likely cause of your server 500 error.

学习基本的 php:$['true'] = 'true';是一个明显的语法错误。这是您的服务器 500 错误最有可能的原因。

回答by Niek van der Steen

You made a typo:

你打错了:

$['true'] = 'true';

Should probably be

应该是

$a['true'] = true;

(note the $ a)

(注意 $ a

Also note that whether your login would succeed or not, it will always fill that 'true value' with true. Looking at your Javascript, that's not what you want. Consider setting it on falsein your else statement.

另请注意,无论您的登录是否成功,它都会始终用true填充“真实值” 。看看你的 Javascript,这不是你想要的。考虑在您的 else 语句中将其设置为false

回答by Victor

Add string like that to see errors in your php script:

添加这样的字符串以查看 php 脚本中的错误:

error_reporting(E_ALL);
ini_set('error_log', 'path_to_log_file');
ini_set('log_errors_max_len', 0);
ini_set('log_errors', true);