【问题标题】:jQuery $.get() JSON function executing but won't result anythingjQuery $.get() JSON 函数正在执行但不会产生任何结果
【发布时间】:2012-08-08 16:04:14
【问题描述】:

执行$.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"=&gt;"true"));

感谢大家的宝贵时间。

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

Edit2:我还尝试将echo 调用更改为echo true;return true;

Edit3:我可以在 Chrome Inspector 和 Firebug 的 Network 标签中看到 PHP 页面的结果:{"result":"false"}array(1) { ["result"]=> string(5) "false" } 它表明问题将存在于$.getJSON() 调用中!但在我看来一切都很好!

【问题讨论】:

  • 使用 Firebug 检查来自服务器的实际响应。
  • 响应来自与网页相同的Origin
  • PHP 脚本出错了吗?浏览器的 JavaScript 控制台中是否出现任何内容?
  • 根本没有反应出来。我什至不能对data 变量执行console.log(),因为函数内的所有内容都没有执行。
  • 所以,根据你的编辑#3,你没有得到有效的 JSON,对吗?当$.getJSON 没有得到有效的 JSON 时,它没有调用它的成功回调,这并不奇怪。

标签: javascript jquery jquery-ui jquery-plugins dialog


【解决方案1】:

如果您将无效的 JSON 传递给 $.getJSON,则不会触发成功回调。正如您在编辑中提到的,您收到的回复类似于 {"result":"false"}array(1) { ["result"]=&gt; string(5) "false" },这肯定不是有效的 JSON。

更正您的脚本以生成有效的 JSON,您应该一切顺利!

【讨论】:

  • 再次感谢您,先生!这可能对我自己有用!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
  • 2021-06-18
  • 1970-01-01
  • 2017-03-18
  • 1970-01-01
  • 2021-09-07
相关资源
最近更新 更多