【问题标题】:jQuery AJAX POST executes twicejQuery AJAX POST 执行两次
【发布时间】:2016-06-03 11:23:21
【问题描述】:

我的 AJAX 调用总是执行两次。我正在使用普通的 Javascript 事件处理程序和<input type="button">。我不太确定哪里出错了,服务器返回一个 JSON 数组,AJAX 可以成功接收并解析。我很确定这是我的 AJAX,因为当我运行代码时,警报会弹出两次。 alert("ran")。服务器不返回任何重定向标头。

HTML

<form action="/" onsubmit="return false">
    <b>Enter your announcement here</b>
    <span class="dropdown"><img src="/index/img/p/help" style="width: 20px; height: 20px;"/>
        <span class='dropdown-content'>
            <b>tip:</b> try to make your announcement less than 100 words. Choose what you say wisely.
        </span>
    </span>
    <textarea class='form-control' id='announcetxtbox' style='resize: vertical'></textarea>
    <select id="announce-type">
        <option value="general">General</option>
        <option value="social">Social</option>
        <option value="world">World</option>
        <option value="maint">Site Maintenence</option>

    </select>
    <input type="button" id="announce-submit" class="btn btn-primary" value="Submit">

    <script>
        CKEDITOR.replace("announcetxtbox");
        CKEDITOR.config.toolbar = [
            {name: 'basicstyles', items: ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat']},
        ];
    </script>

</form>

JAVASCRIPT

function _(str) {
    return d.getElementById(str);
}
function get_announcement_data(ele) {

    var type, answer = _("announce-msg");
    switch (ele) {
        case "general":
            type = "G";
            break;
        case "social":
            type = "S";
            break;
        case "world":
            type = "W";
            break;
        case "maint":
            type = "M";
            break;
        default:
            type = "G";
            break;
    }

    $.ajax({
        url: "/profile_scripts/announcements.php?u=get",
        type: "POST",
        data: {type: type, CSRF_TOKEN: _("CSRF_TOKEN_VALUE").value},
        success: function (data) {
            data = JSON.parse(data);
            answer.innerHTML = "";
            for (var i = 0; i < data.length; i++) {
                if (data[i].response == 1) {
                    answer.innerHTML += data[i].msg + "<small> Written By: " + data[i].author + " on " + data[i].date + "</small>"
                } else {
                    answer.innerHTML = data[i].msg;
                }
            }

        }
    })

}
function add_announcement() { //THIS FUNCTION RUNS TWICE!

    var announcement = CKEDITOR.instances.announcetxtbox.getData();
    var type = _("announce-type").value;
    var code;
    switch (type) {
        case "general":
            code = "G";
            break;
        case "social":
            code = "S";
            break;
        case "world":
            code = "W";
            break;
        case "maint":
            code = "M";
            break;
        default:
            code = "G";
            break;
    }

    $.ajax({
        url: "/profile_scripts/announcements.php?u=post",
        type: "POST",
        data: {type: code, CSRF_TOKEN: _("CSRF_TOKEN_VALUE").value, msg: announcement},
        success: function (data) {
            data = JSON.parse(data);
            if (data.response == 1) {
                alert("ran");
                get_announcement_data(type);

            } else {

                alert("Something went wrong!");

            }

        }
    });

}
d.addEventListener("DOMContentLoaded", function () {

    _("quick-actions-go").addEventListener("click", quick_action_search)

    _("manual-data-pull").addEventListener("click", pull_data);


    _("announce-submit").addEventListener("click", function (e) {

        add_announcement(); <--- Event Listener
        e.preventDefault();
        e.stopPropagation();
    });

    var tab_elements = d.querySelectorAll("[id=ann-tab]");

    for (var i = 0; i < tab_elements.length; i++) {
        tab_elements[i].addEventListener("click", function (e) {
            e.preventDefault();
            get_announcement_data(this.dataset.modeval)
        });
    }


});

【问题讨论】:

  • 看起来announce-submit click 处理程序被您的程序触发了两次。您可以检查开发工具上的堆栈跟踪以查看它的源代码。

标签: javascript jquery json ajax


【解决方案1】:

这里您的表单提交了两次。你可以 1. 在调用 fun add_announcement 时传递 'e'。 Aad 添加行

e.preventDefault();

作为 fun add_announcement(e) 的第一行。

  1. 另外,从表单中取消绑定提交操作

    $('form').unbind('submit').submit();
    

希望对你有帮助。

【讨论】:

【解决方案2】:

提交表单时。两个ajax都会调用。你先get方法和post方法导致问题

【讨论】:

  • $( "#announce-submit" ).click(function(e) { add_announcement();
猜你喜欢
  • 1970-01-01
  • 2013-03-08
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-19
  • 2012-09-23
  • 1970-01-01
相关资源
最近更新 更多