非常初级的jquery+ajax

Very elementary jquery + ajax

本文关键字:jquery+ajax 非常      更新时间:2023-09-26

单击按钮,我想捕获表单的数据,并使用ajax、json将其发送到php页面。现在,只有当我评论了我的AJAX部分时,点击按钮就会出现警报"你好你好"。当我通过取消注释来包含ajax部分时,我不仅没有得到任何错误,而且我的"你好你好"警报也不会显示。我觉得这很奇怪。为什么会这样?

这是我的ajax:

<script>
    $(document).ready(function () {
        $("#btn").click( function() {
        alert('hello hello');
        /* Coomenting starts here
        $.ajax({
            url: "connection.php",
            type: "POST",
            data: {
                id: $('#id').val(),
                name: $('#name').val(),
                Address: $('#Address').val()
            }
            datatype: "json",
            success: function (status)
            {
                if (status.success == false)
                {
                    alert("Failure!");
                }
                else 
                {
                    alert("Success!");
                }
            }
        });
        */
    //commenting ends here.
    //basically i just commented out the ajax portion 
    });
    });
</script> 

"data"后缺少逗号:

$.ajax({
    url: "connection.php",
    type: "POST",
    data: {
        id: $('#id').val(),
        name: $('#name').val(),
        Address: $('#Address').val()
    }, // Missing comma!
    datatype: "json",
    success: function (status)
    {
        if (status.success == false)
        {
            alert("Failure!");
        }
        else 
        {
            alert("Success!");
        }
    }
});

正如@dystroy所指出的,由于您的代码无法编译,这个格式错误的块可能会阻止其他代码(如简单警报)启动。

检查数据参数后的逗号语法:

data: {
         id: $('#id').val(),
         name: $('#name').val(),
          Address: $('#Address').val()
      },

data部分后面缺少一个逗号:

<script>
    $(document).ready(function () {
        $("#btn").click( function() {
        alert('hello hello');
        /* Coomenting starts here
        $.ajax({
            url: "connection.php",
            type: "POST",
            data: {
                id: $('#id').val(),
                name: $('#name').val(),
                Address: $('#Address').val()
            },
            datatype: "json",
            success: function (status)
            {
                if (status.success == false)
                {
                    alert("Failure!");
                }
                else 
                {
                    alert("Success!");
                }
            }
        });
        */
    //commenting ends here.
    //basically i just commented out the ajax portion 
    });
    });
</script>