jQuery $.get() JSON 函数正在执行,但不会产生任何结果

jQuery $.get() JSON function executing but won't result anything

本文关键字:结果 任何 执行 get JSON 函数 jQuery      更新时间:2023-09-26
每次

我调用脚本时(当用户单击链接/按钮时),执行$.getJSON()时调用的页面都在执行其工作,尽管与之关联的函数不会执行任何操作! 这是在Script.php文件中:

$('a.addCategorie').click(function (e)
    {
        e.preventDefault();
        var dialog='<div id="Dialog_AddCategory">'
        <div id="tableContainer">'
        <table class="categoryTable">'
        <thead>'
        <tr>'
        <th>Ordre</th>'
        <th>Catégorie</th>'
        <th>    </th>'
        </tr>'
        </thead>'
        <tbody>'
        <?php 
            $sql="SELECT nom, ordre FROM category ORDER BY ordre";
            $result=mysql_query($sql);
            while($row=mysql_fetch_array($result))
            {
                echo "<tr>";
                echo "<td>". $row['ordre'] ."</td>";
                echo "<td>". utf8_decode($row['nom']) ."</td>";
                echo "<td id='"" . $row['ordre'] . "'" class='"deleteCat'"></td>";
                echo "</tr>'''n";
            }
        ?>
        </tbody>'
        </table></div>'
        <div id="addCategorie_Form">'
        <form>'
        <label for="nomCategorie">Nom de la catégorie</label>'
        <input type="text" name="nomCategorie" id="nomCategorie"/>'
        <label for="ordreCategorie">Ordre</label>'
        <input type="text" name="ordreCategorie" id="ordreCategorie"/>'
        </form>'
        </div></div>';
        $('body').append(dialog);
$( '#Dialog_AddCategory' ).dialog({
                autoOpen: false,
                modal: true,
                width: 800,
                height: 400,
                open: function(even, ui) { $(".ui-dialog-titlebar-close", ui.dialog).css("visibility","hidden");},
                title: "Nouvelle catégorie",
                resizable: false,
                hide:'slide',
                show:'slide',
                buttons:
                {
                    "Créer la catégorie":function()
                    {
                        var ok = true;
                        if(isNaN($('#ordreCategorie').val()) || $('#ordreCategorie').val().length < 1)
                        {
                            ok = false;
                            $('#ordreCategorie').css("background-color","#F00");
                        }
                        else
                        {
                            $('#ordreCategorie').css("background-color","#CF0");
                        }
                        if($('#nomCategorie').val().length< 3)
                        {
                            ok = false;
                            $('#nomCategorie').css("background-color","#F00");
                        }
                        else
                        {
                            $('#nomCategorie').css("background-color","#CF0");
                        }
                        if(ok)
                        {
                            var ordre = $('#ordreCategorie').val();
                            var nom = $('#nomCategorie').val();
                            $.getJSON('addCategory.php', {'ordre':ordre,'nom':nom}, function(data) 
                            {
                                console.log("THIS LOG WON'T APPEAR AND THE CODE WON'T EXECUTE.");
                                if( data.result === "false" )
                                {
                                    $('div id="Dialog_Feedback">Une catégorie porte déjà ce nom ou cet ordre!</div>').dialog(
                                    {
                                        autoOpen:false,
                                        title:'Une erreur est survenue!',
                                        width:200,
                                        height:'auto',
                                        resizable: false,
                                        modal:true,
                                        buttons:
                                        {
                                            "OK" : function()
                                            {
                                                $( this ).remove();
                                            }
                                        }
                                    });
                                }
                                else
                                {
                                    $('<div id="Dialog_Feedback">L''ajout a été effectué avec succès!</div>').dialog({
                                        autoOpen:false,
                                        title:'Catégorie ajoutée!',
                                        width:400,
                                        height:'auto',
                                        resizable:false,
                                        modal:true,
                                        buttons:{
                                            "Ok": function()
                                            {
                                                $(this).remove();
                                                window.location.reload();
                                            }
                                        }
                                    });                                 
                                }
                                $('#Dialog_Feedback').dialog("open");   
                            });
                        }
                    },
                    "Annuler":function()
                    {
                        $( this ).remove();
                    }
                }   
        });

这是addCategory.php页面:

<?php
include('../../anything.php');
$nom = $_GET['nom'];
$ordre = $_GET['ordre'];
$sql = "SELECT ordre, nom FROM category";
$checking = mysql_query($sql);
$ok = true;
while ($row=mysql_fetch_array($checking))
{
    echo "test";
    if((strtolower($nom) === strtolower($row['nom'])) || ($ordre === $row['ordre']))
    {
        $ok = false;
    }
}
if ($ok)
{
    $sql = "INSERT INTO category (nom,ordre) VALUES('$nom',$ordre)";
    $result = mysql_query($sql);
    mysql_close($connexion);
    echo json_encode(array("result"=>"true"));
}
else
{
    echo json_encode(array("result"=>"false"));
}
?>

有人知道是什么原因造成的吗?我已经检查了GET中发送的两个变量,它们包含一些东西。

我的 PHP 页面返回如下 JSON 编码结果:

echo json_encode(array("result"=>"true"));

谢谢大家的时间。

编辑:我忘了提到Chrome Inspector和Firebug在整个脚本执行中都没有报告任何错误。console.log()部分也没有出现,这意味着 PHP 页面中的命令被执行,但$.get()中包含的 javascript 函数没有被触发。

编辑2:我还尝试将echo调用更改为echo true;return true;

编辑3:我可以在Chrome Inspector和Firebug的"网络"选项卡中看到PHP页面的结果:{"result":"false"}array(1) { ["result"]=> string(5) "false" }这表明问题将存在于$.getJSON()调用中!但对我来说一切似乎都很好!

如果将无效的 JSON 传递给 $.getJSON ,将不会触发成功回调。 正如您在编辑中提到的,您得到的回复是 {"result":"false"}array(1) { ["result"]=> string(5) "false" } ,这当然不是有效的 JSON。

更正您的脚本以生成有效的 JSON,您应该很高兴!