weixin_33743880 2015-09-29 03:54 采纳率: 0%
浏览 50

意外令牌<错误json

I've been getting this error and and for the life of me i can't figure out what exactly is causing it. What I'm trying to do is retrieve some data from a database and display it on a page but I'm not able to.

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

and this is the file where im doing the retrieval at

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

the console points to the line "var json = JSON.parse(xmlhttp.responseText);" as to where the error is at. I apologise if the solution seems a tad obvious, I've only just started dabbling in this. Thank you for any help provided.

  • 写回答

1条回答 默认 最新

  • weixin_33727510 2015-09-29 04:25
    关注

    Here is a close proximity to your ajax. I don't have all the other stuff that your js is referencing, so portions of this will fail as is:

    <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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 webstorm上开发的vue3+vite5+typeScript打包时报错
  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥100 求ASMedia ASM1184e & ASM1187e 芯片datasheet/规格书
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL