如何将Ajax响应转换为字符串以与JS字符串进行比较

时间:2018-08-11 09:06:27

标签: javascript php ajax

我如何将Ajax响应转换为纯文本字符串?我有全局变量,并且存储了对它的ajax响应,但是当我将它们与javascript字符串进行比较时,即使它们相等也返回false。

这是我的代码:

function checkUsn(){
    var usn = document.getElementById("usn").value;
    if(usn){
        $.ajax({
            type: 'post',
            url: 'checkdata.php',
            data: {
                emp_username: usn,
            },
            success: function(response){
                console.log(response);
                myGlobalContainer.usn = response; //convert it to compare with string
                $('#status').html(response);
            }
        });
    }
}
在控制台中

,当我在数据库中键入现有的用户名时,它将记录为OK。这个OK存储在myGlobalContainer.usn中,但是当我像下面的代码那样进行比较时,它返回false。

if(myGlobalContainer.usn == "OK"){
return true;
}else{
return false;
}

我将添加php文件。

<?php
header("Content-Type: text/plain");
include 'db_config.php';
$conn = new mysqli($db_servername, $db_username, $db_password, $db_name);

if(isset($_POST['emp_username'])){
    $usn = $_POST['emp_username'];

    $checkdata = "SELECT emp_username FROM emp_details where emp_username='$usn'";

    $query = mysqli_query($conn, $checkdata);

    if(mysqli_num_rows($query) > 0){
        echo "OK";
    }else{
        echo "Your Username not exist";
    }
    exit();
}

if(isset($_POST['emp_pw']) && isset($_POST['emp_usn'])){
    $pw = $_POST['emp_pw'];
    $usn = $_POST['emp_usn'];

    $get_pw = "SELECT emp_password FROM emp_details where emp_username='$usn'";

    $query = mysqli_query($conn, $get_pw);

    //$get_num_rows = mysqli_num_rows($query);
    //echo $get_num_rows;

    $row = mysqli_fetch_assoc($query);
    //echo $row["emp_password"];

    // check if password is match with username
    if($pw == $row["emp_password"]){
        echo "MATCH";
    }else{
        echo "Wrong password";
    }
    exit();
}
?>

请帮助谢谢!

3 个答案:

答案 0 :(得分:1)

默认情况下,jQuery的ajax函数将确定从Content-Type响应标头接收的数据类型。

您可以使用dataType参数覆盖它。

$.ajax({
    dataType: "text",
    // etc etc
});

…但是,由于响应似乎是“ OK”而不是HTML,因此可能需要对PHP进行调整,使其输出正确的Content-Type:

<?php
    header("Content-Type: text/plain"); # Override the default (text/html)
    echo "OK";

因此,还要确保响应确实只是“ OK”,并且您没有输出(例如)“ OK”后跟新行。

答案 1 :(得分:0)

我似乎应该在ajax成功中使用一个函数。

var myGlobalContainer.usn = "";
function signAndCompare(str)
{
     myGlobalContainer.usn = str
     if(myGlobalContainer.usn == "OK")
         {
             console.log("true");
             return true;
          }
     console.log("false");
     return false;
 }

function checkUsn(){
var usn = document.getElementById("usn").value;
if(usn){
    $.ajax({
        type: 'post',
        url: 'checkdata.php',
        data: {
            emp_username: usn,
        },
        success: function(response){
            console.log(response);
            signAndCompare(response);//this line: **compare** and sign response
            $('#status').html(response);
        }
    });
}

答案 2 :(得分:0)

我将代码更改为

    success: function(response){
        console.log(response);
        myGlobalContainer.usn = response.trim(); //convert it to compare with string
        $('#status').html(response);

并且它可以正常工作,但是Guys非常感谢您的帮助! 也感谢这个问题 Ajax response doesn't equal what I think it should

相关问题