大规模的jQuery更新 - >我的ajax不再起作用了

时间:2013-08-11 07:53:55

标签: php json jquery updates

我几个月来一直在使用stackoverflow,因为它确实是我能想到的最可靠的资源。但是,我现在有了自己的问题。

我正在编写一个脚本,在调用一些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 / appel-tableau-ajax.php中的PHP代码。放置在成功案例中的Console.log()不显示任何内容。 我正在添加Chrome检测到的语法错误的屏幕截图。如果有人能帮助我理解如何处理这些错误,那就太好了。

Link to the screenshot

另外,Safari的调试器说:SyntaxError:JSON Parse error:意外的标识符“object”。 我无法相信所有这些都适用于旧的jQuery版本......

非常感谢,我希望我不要求太多!

1 个答案:

答案 0 :(得分:0)

如果你的php代码返回json为什么你的ajax中的'dataType'是'script'?尝试将其改为json。 希望它能奏效! :)