weixin_33717298 2016-04-23 05:32 采纳率: 0%
浏览 57

jQuery Ajax触发两次

for some reasons the function is being triggered twice. also if you can help me improve the codes (am a noobz) that would be highly appreciated.

the main calling function will be: get_range_entries

once the callback returns the status it displays the modal box but somehow it goes to the next lines of code so its preventing me from closing the modal window.

function load_XMLDoc(dname, callback) {
    $.ajax({
        cache: false,
        type: 'GET',
        dataType: "xml",
        url: dname + '&date=' + new Date().getTime(),
        success: function(result) {
            if(typeof callback === "function") {
                callback(result)
            }
        }
     })
}


function get_range_entries(view_name, startkey, universal_id, column, callback) {
    //unid = universal id
    //column = column number to compare
    //this is for uncategorized views only

    var url;
    var status = false;

    url = view_name + '?ReadViewEntries&ResortAscending=' + column + '&StartKey=' + startkey + '&UntilKey=' + startkey + 'Z&ExpandView';
    load_XMLDoc(url, function(result) {
        viewentries = result.getElementsByTagName("viewentries");
        entries = result.getElementsByTagName("viewentry");
        range_entries = viewentries[0].getAttribute('rangeentries');

        if (range_entries == 0) {
            status = false;
        }
        else {          
            entries = result.getElementsByTagName("viewentry");
            columns  = entries[0].getElementsByTagName("entrydata");

            if (entries[0].getAttribute('unid')) {
                unid = entries[0].getAttribute('unid');
            }

            if (unid == universal_id)
                status = false;
            else {
                colvalue = columns.item(column).childNodes[1].firstChild.nodeValue;
                status = (colvalue.toLowerCase() == startkey.toLowerCase());
            }
        }
        alert(1); ------------------> just using this to check if its being triggered twice
        callback(status);
    })
}



function display_message_string(message_title, message_string, hide_primary) {  
    $('#modal-confirm').find('.modal-title').html(message_title);
    $('#modal-confirm').find('.modal-body').html(message_string);

    if (hide_primary)
        $('#modal-confirm').find('.btn-primary').hide();
    else
        $('#modal-confirm').find('.btn-primary').show();

    $('#modal-confirm').modal('show');      
}



get_range_entries(view_name, startkey, unid, 1, function(status) {
        if (status) {
            display_message_string("DUPLICATE ENTRIES ENCOUNTERED", "The GENERATOR NAME you are trying to save already exists in the Database ... Please try again", true);
            return false; ----> added this just to prevent it from triggering twice but it doesn't seem to work
        }
})

..... next lines of code to save the form

  • 写回答

0条回答 默认 最新

    报告相同问题?