意外的令牌<错误json

时间:2015-09-29 03:54:09

标签: php json ajax

我一直在接受这个错误,并且对于我的生活,我无法弄清楚究竟是什么导致了它。我要做的是从数据库中检索一些数据并将其显示在页面上,但我无法做到。

xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        if (xmlhttp.responseText != null || xmlhttp.responseText != "That email does not exist in our database") {
            var json = JSON.parse(xmlhttp.responseText);
            var image = json[0]; // or json["Data"]
            document.getElementById("dp").innerHTML =  image;
        } else {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
}

xmlhttp.open("GET", "getemail.php?q=" + str, true);
xmlhttp.send();

这是我在

进行检索的文件
$q = $_GET['q'];

$data = [];
if (!$link) {
    die('Could not connect: ' . mysqli_error($link));
}

$sql="SELECT officer_name FROM officer WHERE email = '$q'";
$result = mysqli_query($link,$sql);
$getName = mysqli_fetch_assoc($result);
$name = $getName['officer_name'];

if($name == null){
    echo "That email does not exist in our database";

} else {
    $sql2="SELECT picture, officer_name FROM officer WHERE email = '$q'";
    $result2 = mysqli_query($link,$sql2);
    $getItems = mysqli_fetch_array($result2);
    $pic = $getItems['picture'];
    $name = $getItems['officer_name'];
    $data = ["image" => "<img style='height:150px; width:150px;' src='image/" . $pic . "'>", "email" => "$q"];
    echo $data;
}

mysqli_close($link);

控制台指向行“var json = JSON.parse(xmlhttp.responseText);”至于错误的位置。如果解决方案看起来有点明显,我很抱歉,我刚开始涉足这个问题。感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:2)

这是一个非常靠近你的ajax。我没有你的js所引用的所有其他内容,因此部分内容将失败:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script>
$(document).ready(function() {
    $("#clicker").click(function() {
        var str =   'test';
        $.ajax({
                url: "getemail.php",
                type: 'get',
                data: { q: str },
                success: function(response) {
                        if(response != null)
                            $("#dp").html(response);
                        else
                            $("#txtHint").html("That email does not exist in our database");
                    }
        });
    });
});
</script>
<div id="clicker">CLICK</div>
<div id="dp">load into</div>
<div id="txtHint">txtHint text hing</div>