简体   繁体   English

在AJAX调用后使用JQuery更新div

[英]use JQuery to update a div after AJAX call

我正在使用AJAX调用删除和正在运行的项目,我遇到的麻烦是更新div中的数据表以反映从数据库中删除项目。

So here the short answer: 所以这里是简短的答案:

 if(itemdel=="deleted")
 {
     console.log("get request to datatable");
     //add the absolute url here so with http://xyz.xy/includes/datatable.php
     $.get('includes/datatable.php', function( data ) {
         console.log("data received, data");
         $('#dataTable').html(data);
     });
 }

Here the "long one" try to understand :) 这里的“长者”试着去理解:)

var delID = document.getElementById('delID').value;

$('#so-delete').on('click', deleteData); // execute the deleteData function on click // without ()!!! when you put () it will be executed @init

function deleteData() {
    apiCall("includes/deletesoitem.php", "POST", loadData, delID); // makes the request --> loadData function executed after ajax call is successfully finished
}

function loadData(itemdel) {
    if (itemdel == "deleted") {
        alert("SUCCESS: Item Deleted Successfully!");
    //  the "anonymous "callBack" function will be executed after ajax request is finished
        apiCall("includes/datatable.php", "GET", function (msg) {
            $('#dataTable').html(msg);
        });
    }
}

/**
 *
 * @param url Request for the URL
 * @param ajaxMethod Request Method (POST/GET/ETC)
 * @param callBack Callback method this method will be executed after     ajax request is successful
 * @param data Request body data
 */
function apiCall(url, ajaxMethod, callBack, data) {
    var requestData = (data != "undefined") ? data : {}; // set request data to data if exists

    var request = $.ajax({
        url: url,
        method: ajaxMethod,
        data: requestData,
        dataType: "text"
    });

    request.done(function (msg) {
        //execute the callback function
        callBack(msg);
    });

    request.fail(function (jqXHR, textStatus) {
        alert("Request failed: " + textStatus);
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM