我已经使用stackerflow好几个月了,因为它确实是我能想到的最可靠的资源。然而,我现在有自己的问题。
我正在编写一个脚本,在调用一些php/MySQL交互后,使用ajax在HTML5页面中显示可用的课程。我使用的是jQuery 1.4.1(是的…我知道),由于有一些有趣的新插件可用,我更新到了1.10.2。
现在,我的页面不再显示PHP/SQL处理的结果,自动滚动也不起作用。我搜索了一下,没有找到任何答案。我希望你们能直接看到我代码中的不兼容,或者使用了太旧的语法。
触发器:
<a class="internal-link" href="#" onclick="goToByScroll('reanimation');"></a>
脚本:
function goToByScroll(id){
$.ajax({
type: "POST",
url: "kernel/appel-tableaux-ajax.php",
dataType: "script",
data:{type_cours : id},
success: function(array){
var objet = JSON.parse(array);
var message = objet["message"];
var nombre = objet["nombre"];
$("#conteneur-tableaux").html(message);
$("#nombre-cours").html(nombre);
if(nombre==0)
{
$("#titre-section-tableaux").css("color","darkred");
}else{
$("#titre-section-tableaux").css("color","darkgreen");
}
$('html,body').animate({scrollTop: $("#section-tableaux").offset().top},'slow');
}
});
}
PHP代码(我删除了不太可能出错的东西):
ob_start(); // I want to catch all the 'echos' to insert them in an array.
while ($donnees = $reponse->fetch())
{
// Lots of Date comparison stuff and many ECHOS.
}
$out = utf8_encode(ob_get_contents()); // --- I put every echos inside the 'OUT' variable.
ob_end_clean();
// --- I want to send back 2 things to my script, some infos about the courses available, and the total number of available courses. So I create an array.
$output_final = array ("message" => $out, "nombre" => $compteur_cours);
echo json_encode($output_final);
?>
正如您所看到的,我是Ajax和jQuery使用的初学者。
更新:
非常感谢所有已经提供帮助的人。我在Chrome中尝试了调试工具,php生成了它应该做的事情,这意味着我的ajax能够调用放在kernel/apel-tableau-ajax.php中的php代码。放在成功案例中的Console.log()没有显示任何内容。我添加了一张Chrome检测到的语法错误的屏幕截图。如果有人能帮助我理解如何处理这些错误,那就太好了。
屏幕截图链接
此外,Safari的调试器说:SyntaxError:JSON解析错误:意外标识符";对象";。我不敢相信所有这些都在旧的jQuery版本上起作用。。。
非常感谢,我希望我不要要求太多!
如果php代码返回json,为什么ajax中的"dataType"是"script"?尝试将其更改为json。希望它能起作用!:)