Encapsulation of Ajax function in jQuery

$.ajax({
    type : "post",
    url : "workbench/customer/selectAll.do",
    success : function(data){
        if(data.success){
            var html = "";
            $.each( data.customerList, function(i , n){                       
                html += "<tr>";
                html += "<td><input type="checkbox" value=""+n.id+""/></td>";
                html += "<td><a style="text-decoration: none; cursor: pointer;" onclick="window.location.href="workbench/customer/detail.jsp">"+n.name+"</a></td>";
                html += "<td>"+n.owner+"</td>";
                html += "<td>"+n.phone+"</td>";
                html += "<td>"+n.website+"</td>";
                html += "</tr>";
            })                    
            $("-sharpcustomerTbody").append(html);
        }else{
            alert("");
        }
    }
})
,?
Mar.13,2021

just throw it into the function and call it, don't you?

function name(){
    $.ajax({
    type : "post",
    url : "workbench/customer/selectAll.do",
    success : function(data){
        if(data.success){
            var html = "";
            $.each( data.customerList, function(i , n){                       
                html += '<tr>';
                html += '<td><input type="checkbox" value="'+n.id+'"/></td>';
                html += '<td><a style="text-decoration: none; cursor: pointer;" onclick="window.location.href="workbench/customer/detail.jsp">'+n.name+'</a></td>';
                html += '<td>'+n.owner+'</td>';
                html += '<td>'+n.phone+'</td>';
                html += '<td>'+n.website+'</td>';
                html += '</tr>';
            })                    
            $("-sharpcustomerTbody").append(html);
        }else{
            alert("");
        }
    }
})    
};
name();

of course. What do you think is the reason why not?


encapsulate a method:
function ajaxFunc (type, url, customerTbody, eachContent) {

$.ajax({
type : type,
url : url,
success : function(data){
    if(data.success){
        var html = "";
        $.each( data.customerList, function(i , n){                       
           var html = eachContent;
        })                    
        $(customerTbody).append(html);
    }else{
        alert("");
    }
}
})

}

Menu