这是我的问题,我有一个AJAX函数,它在我的本地服务器上运行但是当我把它放在我的在线服务器上时不会返回任何内容。
这是我的代码:
这里是我调用函数showEspece()的页面:
echo "<div class='tableau'>";
echo "<table class='tableAnimal'>\n";
echo "<thead>\n";
echo "<td class='tdAnimal'><b> Nom </b></td>\n";
echo "<td class='tdAnimal'><b> Nombre </b></td>\n";
echo "</thead>\n";
while ($row = oci_fetch_array($requete, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr class='trAnimal'>\n";
foreach ($row as $item) {
echo " <td class='tdAnimal'>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "") . "</td>\n";
} ?>
<td class='tdAnimal' onclick="showEspece(<?php echo "'";echo $row['ESPECE'];echo "'";?>, <?php echo "'";echo $categorie;echo "'";?>,this);" ><a class='lightbox'><img src='images/loupe.png'/></a></td> <?php
echo "</tr>\n";
}
echo "</table>\n\n";
echo '</div>';
这是Ajax函数:
function showEspece(espece, categorie, object)
{
$.ajax({
type : 'POST',
url: 'getespece.php',
data: {espece: espece, categorie: categorie },
dataType: 'json',
success: function(data)
{
alert('ok');
var tableau = data;
$('#output').html(tableau);
},
error: function(xhr, status, error) {
console.log(xhr);
}
});
}
以下是Ajax函数的页面调用:
<?php
include("/includes/connexionBD.php");
include("includes/entetepage.php");
$requete = oci_parse($connect, "SELECT nomA, sexe, datenaissance FROM Animal WHERE categorie = '".$_POST['categorie']."' AND espece = '".$_POST['espece']."' ");
oci_execute($requete);
$table = "<table>\n";
$table .= "<thead>\n";
$table .= "<td><b> Nom </b></td>\n";
$table .= "<td><b> Sexe </b></td>\n";
$table .= "<td><b> Date naissance </b></td>\n";
$table .= "</thead>\n";
while ($row = oci_fetch_array($requete, OCI_ASSOC+OCI_RETURN_NULLS)) {
$table .= "<tr>\n";
foreach ($row as $item) {
$table .= " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "") . "</td>\n";
}
$table .= "</tr>\n";
}
$table .= "</table>\n\n";
echo json_encode($table);
?>
这就是我对康索尔的错误:
VM1130:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Object.error (ajax.js:16)
at i (jQuery.js:2)
at Object.fireWith [as rejectWith] (jQuery.js:2)
at A (jQuery.js:4)
at XMLHttpRequest.<anonymous> (jQuery.js:4)
有人知道问题的来源可以帮到我吗?
抱歉我的英文不好:/
答案 0 :(得分:0)
这里的问题是您的JSON响应格式错误,您可以尝试获取输出JSON并通过linter将其找到问题的实际位置:
我建议不要在PHP中构建HTML(你的表),而是使用Javascript构建它,并将Ajax请求中的数据注入到表中。
答案 1 :(得分:0)
使用以下内容更改php响应代码,
<?php
include("/includes/connexionBD.php");
include("includes/entetepage.php");
$requete = oci_parse($connect, "SELECT nomA, sexe, datenaissance FROM Animal WHERE categorie = '".$_POST['categorie']."' AND espece = '".$_POST['espece']."' ");
oci_execute($requete);
$table = "<table>\n";
$table .= "<thead>\n";
$table .= "<td><b> Nom </b></td>\n";
$table .= "<td><b> Sexe </b></td>\n";
$table .= "<td><b> Date naissance </b></td>\n";
$table .= "</thead>\n";
while ($row = oci_fetch_array($requete, OCI_ASSOC+OCI_RETURN_NULLS)) {
$table .= "<tr>\n";
foreach ($row as $item) {
$table .= " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "") . "</td>\n";
}
$table .= "</tr>\n";
}
$table .= "</table>\n\n";
echo json_encode(array("table"=>$table, JSON_UNESCAPED_SLASHES));
?>
使用以下
更改ajax代码function showEspece(espece, categorie, object)
{
$.ajax({
type : 'POST',
url: 'getespece.php',
data: {espece: espece, categorie: categorie },
dataType: 'json',
success: function(data)
{
alert('ok');
var tbl = $.parseJSON(data);
var tableau = tbl.table;
console.log(tableau);
$('#output').html(tableau);
},
error: function(xhr, status, error) {
console.log(xhr);
}
});
}