我在这个php api中收到一个JSON.parse错误,即使数据库中的数据很好,它只是一个纯文本列,我不知道为什么PHP不解析简单文本的JSON,可以有人协助我下面的代码?
我从api获得状态200
错误:
SyntaxError:JSON.parse:JSON数据第2行第1列的意外数据结尾
api.php
<?php
$servername = "localhost";
$username = "testuser";
$password = "dbpass";
$dbname = "testdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (function_exists($_GET['method'])) {
$_GET['method']();
}
function getAllBanks() {
header('Content-Type: application/json');
$banks_sql = mysql_query("SELECT DISTINCT BANK FROM BANK_MASTER;");
$banks = [];
while ($bank = mysql_fetch_object($banks_sql)) {
$banks[] = $bank;
$banks = json_encode($banks);
echo $_GET['jsoncallback'] . '(' . $banks . ')';
}
}
$conn->close();
?>
以下是我从数据库中获取的数据
更新了新的mysqli_ api
<?php
$servername = "localhost";
$username = "fayaz_api";
$password = "Fyzara$1994";
$dbname = "fayaz_BANK";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
if (function_exists($_GET['method']))
{
$_GET['method']();
}
function getAllBanks()
{
header('Content-Type: application/javascript');
// $banks_sql = mysql_query("SELECT DISTINCT BANK FROM BANK_MASTER;");
$banks = [];
$sql = "SELECT DISTINCT BANK FROM BANK_MASTER";
if ($result = mysqli_query($con, $sql))
{
while ($row = mysqli_fetch_array($result))
{
$banks[] = $row;
}
$banks = json_encode($banks);
echo $_GET['jsoncallback'] . '(' . $banks . ')';
// Free result set
mysqli_free_result($result);
}
}
$conn->close();
?>