user3006683 user3006683 - 5 months ago 13
Ajax Question

two ajax request from one page

Simply I would like to load data from two php page using jquery. I put the below code together, however only the one works not both. Any ideas?

$(document).ready(function() {
loadData();
});

var loadData = function() {
$.ajax({
type: "GET",
url: "second.php",
dataType: "html",
success: function(response){
$(".hm_m_un").html(response);
setTimeout(loadData, 1000);
}

});
};
$(document).ready(function() {
loadData();
});

var loadData = function() {
$.ajax({
type: "GET",
url: "test.php",
dataType: "html",
success: function(response){
$(".rx").html(response);
setTimeout(loadData, 1000);
}

});
};

Answer

You've named them all the same. Try some consolidation

$(document).ready(function() {
    window.setInterval(function() { loadData('second.php', $('.hm_m_un')); }, 1000);
    window.setInterval(function() { loadData('test.php', $(".rx")); }, 1000);
});

var loadData = function(page, ele) {
    $.ajax({    
        type: "GET",
        url: page,             
        dataType: "html",                   
        success: function(response){                    
            ele.html(response); 
        }

    });
};