簡體   English   中英

jQuery AJAX POST執行兩次

[英]jQuery AJAX POST executes twice

我的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)
        });
    }


});

提交表格時。 兩個ajax都會調用。 您首先獲取導致問題的方法和發布方法

您的表單在這里提交了兩次。 您大約1.在調用fun add_announcement時傳遞“ e”。 AAD添加線

e.preventDefault();

作為有趣的第一行add_announcement(e)。

  1. 另外,從表單取消綁定提交活動

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

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM