简体   繁体   English

HTML(和jQuery)的相应Ajax调用

[英]Consequtive Ajax Calls from HTML (and jQuery)

I am writing a reporting tool that allows users to check certain servers with a single click on their mouses. 我正在编写一个报告工具,使用户可以通过单击鼠标来检查某些服务器。

Basically, I have a PHP script called check_server.php . 基本上,我有一个名为check_server.php的PHP脚本。 This file needs 1 parameter, named serverNode. 该文件需要1个参数,名为serverNode。 For example check_server.php?serverNode=1234 . 例如check_server.php?serverNode=1234 This script returns either true or false. 该脚本返回true或false。 False means that the server is down. False表示服务器已关闭。

Now, what I am trying to achieve is to have a simple 2-column table with all the server-nodes listed in the first column. 现在,我想要实现的是拥有一个简单的2列表,其中所有服务器节点都列在第一栏中。 When the user clicks "report", then I want to make a Ajax call for every row in the table. 当用户单击“报告”时,我想对表中的每一行进行Ajax调用。 So, first check_server.php?serverNode=1 , then check_server.php?serverNode=5 etc. etc. Then a success or failed icon is shown for every row. 因此,首先是check_server.php?serverNode=1 ,然后是check_server.php?serverNode=5等。然后,每行显示成功或失败图标。

How can I do this? 我怎样才能做到这一点? The next node may only be checked, if the previous one returns true of false. 如果前一个节点返回true或false,则只能检查下一个节点。 Does somebody have an (clear) example? 有人有一个(明确的)例子吗? Thanks in advance! 提前致谢!

I don't know whether I understood your question rightly or not, but I am thinking that you are trying to send one ajax request after completing other ajax request. 我不知道我是否正确理解了您的问题,但我认为您正在尝试在完成其他ajax请求之后发送一个ajax请求。 If that is right you can try this one. 如果那是正确的,您可以尝试这个。

Every ajax request will have some states like before, after, on complete etc.. 每个ajax请求都会有一些状态,例如之前,之后,完成等。

Define your ajax request with one jQuery variable like 用一个jQuery变量定义ajax请求,例如

var xyz = $.ajax({
   ---
   ---
})

Now you can check like this 现在您可以像这样检查

if( xyz.readyState == 1 ) { /* send other ajax request */ }

This automatically sends one more ajax request when first one completed. 第一个请求完成后,它会自动发送另一个ajax请求。

Maybe you can have something like this (DISCLAIMER: I didn't test it): 也许您可以有这样的内容(免责声明:我没有测试过):

var nodeIDs = [1, 2, 3, 5, 9, 10];

function checkNextNode() {
    // no more nodes to check
    if (nodeIDs.length === 0)
    {
        return;
    }

    // get first nodeId in the list
    var nextNode = nodeIDs.shift();

    // make an AJAX call to check this nodeID
    $.ajax({
        type: 'GET',
        url: 'check_server.php',
        data: { serverNode: nextNode },
        success: function onSuccess(result) {
            // if the AJAX call is a success, do something useful with result
            // ex: display it in the table 

            // then check the next node
            checkNextNode();
        }
    });
}

// when the user click on the "report" element, call the "checkNextNode" function
yourReportElement.on('click', checkNextNode);

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

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