Ajaxfunction在本地但不在线工作

这是我的问题,我有一个AJAX函数,它在我的本地服务器上运行,但当我把它放在我的在线服务器上时不返回任何内容。

这是我的代码:

这里我调用函数showEspece()的页面:

echo "
"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; while ($row = oci_fetch_array($requete, OCI_ASSOC+OCI_RETURN_NULLS)) { echo "\n"; foreach ($row as $item) { echo " \n"; } ?> <td class='tdAnimal' onclick="showEspece(, ,this);" > <?php echo "\n"; } echo "
Nom Nombre
" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "") . "
\n\n"; echo '
';

这是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 = "\n"; $table .= "\n"; $table .= "\n"; $table .= "\n"; $table .= "\n"; $table .= "\n"; while ($row = oci_fetch_array($requete, OCI_ASSOC+OCI_RETURN_NULLS)) { $table .= "\n"; foreach ($row as $item) { $table .= " \n"; } $table .= "\n"; } $table .= "
Nom Sexe Date naissance
" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "") . "
\n\n"; echo json_encode($table); ?>

这就是我对康索尔的错误:

 VM1130:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () 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. (jQuery.js:4) 

有人知道问题可以来自哪里,可以帮助我吗?

抱歉我的英文不好:/

这里的问题是您的JSON响应格式错误,您可以尝试获取输出JSON并通过linter将其找到问题的实际位置:

https://jsonlint.com/

我建议不要在PHP中构建HTML(您的表),而是使用Javascript构建它并将Ajax请求中的数据注入表中。

用以下内容更改php响应代码,

 \n"; $table .= "\n"; $table .= " Nom \n"; $table .= " Sexe \n"; $table .= " Date naissance \n"; $table .= "\n"; while ($row = oci_fetch_array($requete, OCI_ASSOC+OCI_RETURN_NULLS)) { $table .= "\n"; foreach ($row as $item) { $table .= " " . ($item !== null ? htmlentities($item, ENT_QUOTES) : "") . "\n"; } $table .= "\n"; } $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); } }); }