Error: JSON.parse: unexpected non-whitespace character after JSON data

五迷三道 提交于 2021-02-11 15:52:08

问题


I have a problem with Json pars, I have seen tons of users had this problem I saw them all but I couldn't understand where my error is in my code! Sorry if this is duplicate!

First file index.html:

This is in the head section of the file:

<script type="text/javascript">
function ajax_json_data(){
    var databox = document.getElementById("databox");
    var field1 = document.getElementById("field1").value;
    var results = document.getElementById("results");
    var x = new XMLHttpRequest();
    x.open( "POST", "test.php", true );
    x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    x.onreadystatechange = function() { 
        if(x.readyState == 4 && x.status == 200){
            databox.innerHTML = "";
            var d = JSON.parse(x.responseText);
            for(var o in d){
                if(d[o].title){
                    results.innerHTML = "";
                    databox.innerHTML += '<p><a href="page.php?id="'+d[o].id+'">'+d[o].title+'</a><br />';
                    databox.innerHTML += ''+d[o].cd+'</p>';
                }
            }
        }
    }
    x.send("limit=4&field1="+field1);
    results.innerHTML = "requesting...";
}
</script>

This is in the body section of the file:

<form id="form" method="post" onSubmit="return false;">
<textarea id="field1" name="field1" placeholder="Input some text here..."></textarea>
<input type="submit" id="submit" name="submit" value="Submit" onClick="ajax_json_data();">
</form>
<div id="results"></div>
<div id="databox"></div>
<script type="text/javascript">ajax_json_data();</script>

The second file test.php:

<?php header("Content-Type: application/json");
if (isset($_POST['field1']) && $_POST['field1'] != "") {
    $field1 = $_POST['field1'];
    require_once("db_conx.php");
    $sqlString = "INSERT INTO test_ajax (title) VALUES ('$field1')";
    $query = mysqli_query($db_conx, $sqlString) or die (mysqli_error()); 
}
if(isset($_POST['limit'])){
    $limit = preg_replace('#[^0-9]#', '', $_POST['limit']);
    require_once("db_conx.php");
    $i = 0;
    $jsonData = '{';
    $sqlString = "SELECT * FROM test_ajax ORDER BY creationdate DESC LIMIT $limit";
    $query = mysqli_query($db_conx, $sqlString);
    while ($row = mysqli_fetch_array($query)) {
        $i++;
        $id = $row["id"]; 
        $title = $row["title"];
        $cd = $row["creationdate"];
        $cd = strftime("%B %d, %Y", strtotime($cd));
        $jsonData .= '"article'.$i.'":{"id":"'.$id.'","title":"'.$title.'","cd":"'.$cd.'"},';
    }
    $jsonData = chop($jsonData, ",");
    $jsonData .= '}';
    echo $jsonData;
}
?>

And mysqli table has an id, a title and a timestamp, I have tested the data in JSONLint and it's completely valid...Maybe some details that I give it's not necessary but nevermind... So it returns this error: SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data ... var d = JSON.parse(x.responseText); Any help would be appreciated! Thanks in advance!


回答1:


Found the problem..the problem was that my host provider send me some code together with my json response so the response from json was invalid..! Thank you for your feedback!



来源:https://stackoverflow.com/questions/22028543/error-json-parse-unexpected-non-whitespace-character-after-json-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!