Я не могу получить доступ к данным json, поскольку он всегда терпит неудачу и дает ошибку как SyntaxError: JSON.parse: неожиданный символ в строке 1 столбца 1 данных JSON search.php выводит данные json, но scripts.js выводит сценарий ошибок json.parse . JS
// execute when the DOM is fully loaded $(function() { console.log("1"); $("#q").typeahead({ autoselect: true, highlight: true, minLength: 1 }, { source: search, templates: { empty: "no places found yet", suggestion: _.template("<p><%- subname %></p>") } }); // re-center map after place is selected from drop-down $("#q").on("typeahead:selected", function(eventObject, suggestion, name) { }); }); function search(query, cb) { // get places matching query (asynchronously) var parameters = { sub: query }; $.getJSON("search.php", parameters) .done(function(data, textStatus, jqXHR) { cb(data); }) .fail(function(jqXHR, textStatus, errorThrown) { console.log(errorThrown.toString()); }); }
search.php
<?php require(__DIR__ . "/../includes/config.php"); $subjects = []; $sub = $_GET["sub"]."%"; $sql = "SELECT * from subjects where subname LIKE '$sub'"; echo $sql; if($rows = mysqli_query($con,$sql)) { $row = mysqli_fetch_array($rows); while($row){ $subjects[] = [ 'subcode' =>$row["subcode"], 'subname' => $row["subname"], 'reg' => $row["reg"], 'courseid' =>$row["courseid"], 'branchid' => $row["branchid"], 'yrsem' => $row["yrsem"] ]; $row = mysqli_fetch_array($rows); } } // output places as JSON (pretty-printed for debugging convenience) header("Content-type: application/json"); print(json_encode($subjects, JSON_PRETTY_PRINT)); ?>
в<?php require(__DIR__ . "/../includes/config.php"); $subjects = []; $sub = $_GET["sub"]."%"; $sql = "SELECT * from subjects where subname LIKE '$sub'"; echo $sql; if($rows = mysqli_query($con,$sql)) { $row = mysqli_fetch_array($rows); while($row){ $subjects[] = [ 'subcode' =>$row["subcode"], 'subname' => $row["subname"], 'reg' => $row["reg"], 'courseid' =>$row["courseid"], 'branchid' => $row["branchid"], 'yrsem' => $row["yrsem"] ]; $row = mysqli_fetch_array($rows); } } // output places as JSON (pretty-printed for debugging convenience) header("Content-type: application/json"); print(json_encode($subjects, JSON_PRETTY_PRINT)); ?>