0

I found some similar questions, but no solution has solved this problem.

function loadProject(id) {
    $.ajax({
        url: 'loadDrumsetData.php',
        type: 'GET',
        data: {
            i: id
        },
        dataType: 'JSON',
        success: function (e) {
            pushLoadedData(e.bank); //create the JavaScript array
        },
        error: function (request, textStatus, errorThrown) {
            console.log(request, textStatus, errorThrown);
        }
    });
}

I try to get the array result from loadDrumsetData.php. On my local apache it works fine. But on my webserver (apache) I get this parser error:

SyntaxError: JSON.parse: unexpected end of data

The loadDrumsetData.php:

<?php
header('Content-type: application/json; charset=UTF-8');
error_reporting(-1);

$i=$_GET["i"];

$con=mysqli_connect("localhost","userxxx","xxxxx","drumpcdata");

if (mysqli_connect_errno())

  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con," SELECT * FROM `session_".$i."` ORDER BY `pID` ASC ");

$soundArray = array();
$bankArray = array();

while($row = mysqli_fetch_array($result))
  {
    $pid = $row['pID'];
    $r = $row['Row'];
    $sound = $row['Sound'];
    $number = $row['Number'];
    $x = $row['X'];
    $y = $row['Y'];
    $w = $row['W'];
    $h = $row['H'];
    $spr = $row['Sprite'];
    $pressed = $row['Pressed'];
    $bankArray = [];
    array_push($soundArray, [$pid, $r, $sound ,$number,$x,$y,$w,$h,$spr, $pressed]);
    array_push($bankArray, $soundArray);
  }

mysqli_close($con);

$encoded = json_encode(array("bank" => $bankArray));
echo $encoded; 
?>

I would appreciate any help on this topic. Any ideas what the problem is?

1
  • 1
    Can you show us the returned JSON? If you have a look at the server response, it should be quite obvious. Commented Apr 2, 2013 at 13:46

1 Answer 1

1

Are you sure you are connecting to mysql database correctly?

First this does not stop processing if connection fails:

if (mysqli_connect_errno())
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

And tries to do queries etc...

Since on failure, your Server sends strings instead of json, It will blow things up.

if (mysqli_connect_errno()){
   echo json_encode(
            array(
                "Success" => false,
                "Reason" => "Failed to connect to MySQL: ".mysqli_connect_error()
            )
        );
   exit(0);
}

Please try adding some failure control to your scripts.

Query may fail too.

Sign up to request clarification or add additional context in comments.

4 Comments

the connection to the MySQL database works. I include it on the Web server from separate PHP file. This works in other places, for example When uploading data.
Can you please show us what do you expect as the output and what do you get?
i want to get data from MySql table to PHP array, to rebuild with this a earlier saved JavaScript array. saving the data in MySql database works fine. but when loading, i get only this output: Object { readyState=4, status=200, statusText="OK"} parsererror SyntaxError: JSON.parse: unexpected end of data parsererror return window.JSON.parse( data ); when i try to load or save on my local apache server (wamp), all works fine.
Please look at api.jquery.com/jQuery.ajax , it says 'json' for instance not 'JSON' check your ajax jQuery...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.