0

today I have been working on a web application where my javascript sends a get request to a php file which handles database communication and replies with an array encoded in json.

But for some reason when I try to use JSON.parse on the response object of the get request I get

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

From what I have managed to find by googleing around this means that there is something wrong with the json formatting. But I can't seem to be able to determine what is wrong.

.js code

var oReq = new XMLHttpRequest(); //New request object
oReq.onload = function() {
        alert(this.responseText);
        var str = this.responseText;
        var total = JSON.parse(str);
        alert(total); 
        alert(total[0]);

};
var id = get('id'); //calls to a different function to retrive the get data
var ar = get('ar');
oReq.open("get", "getdata.php?id=" + id +"&ar=" + ar, true);
oReq.send();

getdata.php

<?php
header('Content-Type: application/json');
require 'connect.php';
if(isset($_GET["id"])){
   $id = $_GET["id"];
   $ar = $_GET["ar"];

   if($stmt = $conn->prepare("SELECT id, nummer, navn, alder, rase, eier, adresse, postnummer, sted, email, tlf FROM hoppe WHERE id = ?")) {
      $stmt->bind_param("s", $id);
      $stmt->execute();
      $stmt->bind_result($unikid, $nummer, $navn, $alder, $rase, $eier, $adresse, $postnummer, $sted, $email, $tlf);

      while ($stmt->fetch()) {
        $unikid = (string)$unikid;
        $data = array($navn,$nummer,$alder,$adresse,$postnummer,$sted,$email,$tlf,$eier,$unikid);
      }

   $stmt->close();

   }
}
echo json_encode($data,JSON_UNESCAPED_UNICODE); 
//echo json_encode($data); 
?>

Using json_encode without JSON_UNESCAPED_UNICODE makes no difference on the syntax error. If I do not use it, special characters like æøå will be displayed as "\u00e5" or "\u00f8".

The direct output of getdata.php if I access it through a web browser is:

["Leif","456","1970-01-01","Bakken1","4867","Oslo","lf@kgo.com",864654,"Kåre","12"]

If I copy this output and place it directly into my javascript then my JSON.parse will work as expected.

The database connection has a fixed charset to "UTF-8", and the database uses "utf8_general_ci" so I am fairly sure everything should be in the right format. But if I use a site like json formater & validator it gives me an output which indicated otherwise

Error:Invalid encoding, expecting UTF-8, UTF-16 or UTF-32.[Code 29,Structure 0]
Error:Expecting object or array, not string.[Code 1, Structure 1]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 1]

But if I again copy paste the direct output I view in my web browser it confirms it as valid.

So, what am I missing?

EDIT 1: Found out that the error was caused by my php files not being encoded in UTF-8 without BOM. Hope this helps someone experiencing the same problem.

Johngear
  • 13
  • 6
  • Most likely there's an `echo` somewhere in your code which messes up the response. – Deividas Mar 12 '17 at 17:08
  • Hmm, there is no echo present in the connect.php and the only thing in my getdata.php which is echoing is the json_encode itself. Are you referring to somewhere else this echo could interfere with the output? – Johngear Mar 12 '17 at 17:16
  • Check this answer: http://stackoverflow.com/questions/4064444/returning-json-from-a-php-script – Deividas Mar 12 '17 at 17:18
  • Thanks, a lot! Turned out that my files where not encoded in UTF-8 without BOM... Everything works like a charm now :) – Johngear Mar 12 '17 at 17:24
  • Happy to help :) – Deividas Mar 12 '17 at 17:24

0 Answers0