Zi-yan Tseng
Zi-yan Tseng

Reputation: 184

Unexpected token in JSON at positon 0

I always get Unexpected token in JSON at positon 0 in my code

the echo json will be

["pen","pencil","apple","cat","dog"]

and the console will get Uncaught SyntaxError: Unexpected token p in JSON at position 0

php code :

    <?php

        include 'Connect_2.php';
        header('Content-Type: application/json;charset=utf-8');
        $Store_int =$_GET['num'];

        #execute sql function and return result
        $sql = "SELECT * FROM `store` WHERE `Place_int` = ".$Store_int;

        mysqli_select_db($link,"web");
        $link -> set_charset("utf8");
        $result = mysqli_query($link,$sql);

        $arr = array();
        while ($row = mysqli_fetch_object($result)){
            $p =(string) $row -> Name;
            $arr[]=$p;
        }
        //print_r($arr);
        $jsonArr = json_encode($arr,JSON_UNESCAPED_UNICODE);

        echo ($jsonArr);
        mysqli_free_result($result);
        mysqli_close($link);
    ?>

.js code

function getArr(store_int){
var jsArray = new Array();
$.ajax({
    url: "fromSQL_store.php",
    data: {
        num: store_int
    },
    type: "GET",
    dataType: "json",
    success: function(data) {
        jsArray = JSON.parse(data);
        //jsArray = data;
    },error: function(data,XMLHttpRequest, textStatus, errorThrown){
        console.log(textStatus);
        console.log(errorThrown);
    }
});
//alert(jsArray.length);
console.log(jsArray[0]);
return jsArray;

}

if I use jsArray = data ;the console(jsArray[0]) will show undefined.

Upvotes: 1

Views: 1098

Answers (2)

Roh&#236;t J&#237;ndal
Roh&#236;t J&#237;ndal

Reputation: 27212

May be your response header Content-Type is text/html instead of application/json.

Hence, If the response header is text/html you need to parse the text into JSON Object but if the response header is application/json then it is already parsed into JSON Object.

The solution is to set correct Content-Type header.

If response header (Content-Type) is text/html :

var jsonObj = JSON.parse(responseData);

If response header (Content-Type) is application/json :

var jsonObj = responseData;

Upvotes: 0

Glen Pierce
Glen Pierce

Reputation: 4821

It's already JSON. Don't call jsArray = JSON.parse(data); Instead, just treat data like a JSON object.

Instead of console(jsArray[0]) call console(jsArray) The data object doesn't seem to be an array.

Also, it looks like you're console.log is being called right after you send the ajax request, which isn't giving you any time to receive the response to populate the object, so it's going to be empty. Put the console.log in the success callback of the ajax request.

JSON.parse() throws this error when it's parsing something that's already JSON which is really annoying since it sure would be nice if it instead just returned the original JSON back to you or at least threw a more precise error like "Error: This is already JSON."

Upvotes: 2

Related Questions