2

I am unable to access json data as it always fails and gives error as SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data search.php outputs the json data but scripts.js outputs json.parse error script.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));

?>
4
  • 1
    What result you are getting? Commented Feb 20, 2017 at 10:06
  • 4
    Have you tried opening your Network browser tab and reviewing results from search.php? It looks like it is not valid JSON, at least since you have echo $sql; at line 6. Commented Feb 20, 2017 at 10:08
  • Another important issue: your code is vulnerable to SQL-injections. Do not use string concatenation for building SQL queries. Commented Feb 20, 2017 at 10:09
  • @YeldarKurmangaliyev - Then how to create query ?? Commented Feb 20, 2017 at 10:21

1 Answer 1

5

Solution for your problem is JSON.stringify

You need to convert your data output into string i.e.,

 var yourDataStr = JSON.stringify(yourdata) 

and then validate your data with

JSON.parse(yourDataStr)

Then you can get your result. To validate, you can pass your data in below function :-

function validateJSON(str) {
    try {
       JSON.parse(str);
    } catch (e) {
     return false;
    }
  return true;
}
Sign up to request clarification or add additional context in comments.

1 Comment

can you please tell me where should I write this. When I write it in .done() then it gives error as missing ) argument list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.