Ajax函数可在本地运行,但不能在线运行 [英] Ajax function working on local but not online
本文介绍了Ajax函数可在本地运行,但不能在线运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的问题,我有一个在本地服务器上运行的AJAX函数,但是将其放在在线服务器上时不返回任何内容.
Here is my problem, I have a AJAX function which is working on my local server but don't return anything when I put it on my online server.
这是我的代码:
在这里我调用函数showEspece()的页面:
Here the page where I call the function 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函数进行的页面调用:
Here is the page call by the Ajax function :
<?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);
?>
这就是我在consol上遇到的错误:
And that is what error I get on the consol :
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)
有人知道问题出在哪里并且可以帮助我吗?
Does someone know where the problem can come from and can help me ?
对不起,我的英语不好:/
Sorry fo my bad english :/
推荐答案
使用以下命令更改php响应代码,
Change php response code with the following,
<?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代码
change ajax code with the following
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);
}
});
}
这篇关于Ajax函数可在本地运行,但不能在线运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文