Unexpected token in JSON at position 0 at JSON.parse (anonymous)

0

I'm trying to make the searchable data in a given main page text field appear without refreshing the page through AJAX.

window.onload = function(){
    document.getElementById("boxsearch").onkeydown = function(e){
        if(e.keyCode == 13){
            var query = this.value==""?"*":this.value;
            enviar(query);
        }
    };

    document.getElementById("btnsearch").onclick = function(){
        var query, tb = document.getElementById("boxsearch");
        query = tb.value==""?"*":tb.value;
        enviar(query);
    };

    function enviar(query){
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState===4&&xmlhttp.status===200){
                var json = JSON.parse(xmlhttp.responseText);
                alert(json[0].titulo);
                var resultados = "";
                for(var i=0; i<json.length; i++){
                    resultados+="<li><h3>"+json[i].titulo+"</h3><p>"+json[i].publicacao+"</p></li>";
                    document.getElementById("result").innerHTML = resultados;
                    document.getElementById("resultnumber").innerHTML = json.length;
                }
            }
        };
        xmlhttp.open("GET","dal.php?val="+query,true);
        xmlhttp.send();
    }
}
    
asked by anonymous 26.06.2017 / 00:08

0 answers