简体   繁体   中英

AJAX call sends data via POST, but PHP doesn't receive

So, I have this really weird problem. I have constructed a web application with a login form on my home pc, but when I try to migrate it to a server with domain, php went all crazy and everything doesn't work anymore now.

So I have a login form, which sends the data via an AJAX POST call to a php file, which compares the given data to the interface. But it seems that in one way or another, the data isn't getting received by the php. Let me show you what my setup is:

the Javascript:

    console.log(username);
    $.ajax({
        url: "server/login_server.php",
        type: "POST",
        datatype: "JSON",
        data: {username: username, password: password},
        complete: function(data) {
            console.log(data.responseText);

            if(data.responseText == "SUCCESS") {
                location.assign("pages/overview.html");
            }else if(data.responseText == "ERROR_CONNECT") {
                showError("kan niet verbinden met server");
            }else if(data.responseText == "ERROR_CODE") {
                showError("Er is iets misgelopen...");
            }else if(data.responseText == "ERROR_FALSE") {
                showError("Inloggegevens ongeldig");
            }else if(data.responseText == "ERROR_MULTIPLE") {
                showError("Er zijn meerdere resultaten gevonden");
            }else if(data.responseText == "ERROR_FORBIDDEN") {
                showError("Dit account heeft geen admin-privileges");
            }

            $("#login_password").val("");
        },
        error: function(data) {
            console.log(data);
        }
    });

Some lines are written in dutch, but don't worry, it doesn't say anything important. The console (on line 1) clearly writes the username, so I know that the username value isn't empty.

The PHP file:

<?php
session_start();

$config = include('config.php');

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

$conn = new mysqli($config['db_host'], $config['db_user'],      $config['db_password'], $config['db_name']);

$result = "SUCCESS";

if($conn->connect_error) {
    $result = $conn->connect_error;
    die($result);
}

$sql = "SELECT * FROM users WHERE userName='".$username."'";
$status = mysqli_query($conn, $sql);

if(!$status) {
    $result = "ERROR_CODE";
    die($result);
}

$num_rows = mysqli_num_rows($status);
$row = mysqli_fetch_array($status);

if($num_rows == 0) {
    $result = "ERROR_FALSE";
    die($username);
}else if($num_rows > 1) {
    $result = "ERROR_MULTIPLE";
    die($result);
}else{
    if($row['isAdmin'] == "false") {
        $result = "ERROR_FORBIDDEN";
        die($result);
    }else{
        $_SESSION['logedIn'] = "true";
        $result = "SUCCESS";
        die($result);
    }
}

echo $username;
?>

It is a simplistic representation of what I am trying to do, but this is where it goes wrong. It gets the username from the post call, and gives it back as data.responsetext to the javascript file.

The javascript file in its turn, prints out an empty string as the result of the php file.

Firebug log:

萤火虫日志

Adding the full error log gives me the following:

"<br />
<b>Notice</b>:  Undefined variable: conn in    <b>/home/netwerklevensein/public_html/dagboek/server/login_server.php</b> on   line <b>8</b><br />
<br />
 <b>Warning</b>:  mysql_real_escape_string() expects parameter 2 to be resource,    string given in    <b>/home/netwerklevensein/public_html/dagboek/server/login_server.php</b> on    line <b>8</b><br />
<br />
<b>Notice</b>:  Undefined variable: conn in    <b>/home/netwerklevensein/public_html/dagboek/server/login_server.php</b> on   line <b>9</b><br />
<br />
<b>Warning</b>:  mysql_real_escape_string() expects parameter 2 to be resource,    string given in <b>/home/netwerklevensein/public_html/dagboek/server/login_server.php</b> on line <b>9</b><br />
" 

I hope you guys can help me.

You're mixing MySQL APIs here with (connecting with mysqli_ then using mysql_ escape functions).

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

that should read as

$username = mysqli_real_escape_string($conn,$_POST['username']);
$password = mysqli_real_escape_string($conn,$_POST['password']);

Those different APIs do NOT intermix.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Check for errors against your query also:

$status = mysqli_query($conn, $sql) or die(mysqli_error($conn));

You can print

echo json_encode(array('username' => $username2));

cause, you set output format is JSON. Another thing, you can use success handler on ajax request, and use only response message (not all XMLHttpRequest response)

I am not a php expert but looks like you are missing some thing on Ajax calls.

Here are my suggestions: To post Json you need to stringify using JSON.stringify(data) Also dont forget to set processdata option to false.

Please place the blow lines inside your ajax call after data: {username: username, password: password},

data: JSON.stringify(data), processData: false,

try this in php,

echo json_encode(
     array('responseText' => $result)
);

You are trying to access responseText property from data object. And echo only $username string

I see in your AJAX code that you said you are receiving JSON data, but in the php file you just issued an echo or die and that ain't JSON data but plain text. So try to put datatype: "text" on the AJAX code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM