jQuery ajax(JSON)从PHP获取数组工作正常,但不在webserver上

时间:2013-04-02 10:39:19

标签: php javascript mysql ajax json

我发现了一些类似的问题,但没有解决方案解决了这个问题。

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);
        }
    });
}

我尝试从loadDrumsetData.php获取数组结果。 在我的本地apache它工作正常。 但是在我的webserver(apache)上,我得到了这个解析器错误:

SyntaxError: JSON.parse: unexpected end of data

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; 
?>

我很感激有关此主题的任何帮助。任何想法是什么问题?

1 个答案:

答案 0 :(得分:1)

您确定要正确连接到mysql数据库吗?

如果连接失败,首先不会停止处理

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

并尝试进行查询等......

由于失败,您的服务器发送字符串而不是json, 它会搞砸。

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

请尝试在脚本中添加一些失败控件。

查询也可能失败。