0

我正在尝试将 3 个 ajax 调用合二为一,但目前我只得到一个。这就是我正在做的事情:

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/Ws.aspx/One",
    dataType: "json",
    success: function (result1) {
        $('#hpl_one').html(result1.d);
    }
}),


$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/Ws.aspx/Two",
    dataType: "json",
    success: function (result2) {
        $('#hpl_two').html(result2.d);
    }
}),


$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/Ws.aspx/Three",
    dataType: "json",
    success: function (result3) {
        $('#hpl_three').html(result3.d);
    }
}),

现在我希望能够调用这三种方法,但目前我只得到一种:

$(document).ready(function () {
    var refreshId = setInterval(function () {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Ws.aspx/One",
            dataType: "json",
            success: function (result1) {
                $('#hpl_one').html(result1.d);
            }
        });
        // end  calling ajax to count alert
    }, 3000);
});

在 HTML 中,我有:

<a id="hpl_one" runat="server">---</a>
<a id="hpl_two" runat="server">---</a>
<a id="hpl_three" runat="server">---</a>

我可以得到hpl_one但我不知道在成功子句中调用hpl_twohpl_three的语法,非常感谢。

4

1 回答 1

1

我能想到的最快的..创建一个包含所有三个调用的函数并调用该函数

function makeAjax()
{
   $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/Ws.aspx/One",
    dataType: "json",
    success: function (result1) {
        $('**#hpl_one**').html(result1.d);
    }
}),


$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/Ws.aspx/Two",
    dataType: "json",
    success: function (result2) {
        $('**#hpl_two**').html(result2.d);
    }
}),


$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/Ws.aspx/Three",
    dataType: "json",
    success: function (result3) {
        $('**#hpl_three**').html(result3.d);
    }
})
}


$(document).ready(function () {
    var refreshId = setInterval(makeAjax, 3000);
});
于 2013-07-16T07:57:10.890 回答