Question: the debugger shows SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON dat How can I fix it table { border-collapse: collapse;
the debugger shows "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON dat" How can I fix it
function ajax_post() { var letter = document.getElementById("input_letter").value; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var to_show; var results = JSON.parse(this.responseText) if (results.length > 0) { to_show = "
| "; to_show += "Email"; to_show += " | "; to_show += ""; to_show += "Password"; to_show += " | "; to_show += ""; to_show += "Birthday"; to_show += " | "; to_show += "
|---|---|---|
| "; to_show += json_result[1]; to_show += " | "; to_show += ""; to_show += json_result[2]; to_show += " | "; to_show += ""; to_show += json_result[3]; to_show += " | "; to_show += "
// Receive input value 'q' from $_POST in a variable $q $q = $_POST["q"];
// Connect to MySQL and save connection in a variable $con // Check for errors in the connection $con = mysqli_connect("localhost", "gaoha202", "JIAYI521","gaoha202");
if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
// Perform a query, check for error if (!mysqli_query($con)) { echo("Error description: " . mysqli_error($con)); }
// Create string variable $sql with SQL selecting users where email LIKE $q // Execute query in MySQL using "$con->query($sql)" and save in $result
$sql = " SELECT * FROM User WHERE email LIKE '$q%' "; $results = $con->query($sql);
// Define empty array $allRows // Iterate through results while $row = $result->fetch_array() // Append $rows to $allRows with $allRows[] = $row
$allRows = array();
while($row = $results->fetch_array()) {
$allRows[] = $row; }
// Echo $allRows in json format using the function json_encode
echo json_encode($allRows);
mysqli_close($con);
?>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
