处理来自 api 回调的未定义变量

Handling undefined variables from api callback

我正在使用 PHP 进行 curl 调用以从外部 api 提取数据。收到的数据类型是json.

在我的 jquery 中,我有固定变量,有时 api 的回调返回时没有这些变量的数据,因此在 Jquery 中它们被视为未定义,因此我的 jquery 语句在到达变量未定义的行时停止。

在web控制台发现如下错误

TypeError: response.endpoints[1] is undefined

有很多变量.. 我可以做些什么来用 "n/a" 全局更新未定义的变量并防止代码停止。期待专家指点。

下面的示例代码

    $.ajax({
    type: "GET",
    url: "api5.php",
    data: dataString,
    dataType: "json",

    //if received a response from the server
    success: function (response) {
            var status = response.status;
            var CVE3389 = "https://community.qualys.com/blogs/securitylabs/2013/09/10/is-beast-still-a-threat?_ga=1.235863681.1412228171.1426286790";
            var CVE0160 = "https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-0160";
            var CVE0224 = "https://community.qualys.com/blogs/securitylabs/2014/04/08/ssl-labs-test-for-the-heartbleed-attack?_ga=1.235863681.1412228171.1426286790";

                if ((status == 'READY' && response.endpoints[0].statusMessage == 'Ready')) {
                // Clear DIV
                $("#ajaxResponsePending").empty();
                // Host details
                $("#ajaxResponse").append("<b>Status:</b> " + response.endpoints[0].statusMessage+ " ["+response.endpoints[0].progress+"%]<br>");
                $("#ajaxResponse").append("<b>Host:</b> " + response.host + ":" + response.port +"<br>");
                $("#ajaxResponse").append("<b>Server:</b> " + response.endpoints[0].details.serverSignature+ "<br>");
                // Vulnerabilities response endpoint 0
                $("#ajaxResponseVul").append("<b>Supports RC4:</b> " +response.endpoints[0].details.supportsRc4+ "<br>");
                $("#ajaxResponseVul").append("<b>Beast:</b> " +response.endpoints[0].details.vulnBeast+ " (CVE-2011-3389) (<a href='"+CVE3389+"' target='_new'>more info</a>)<br>");
                $("#ajaxResponseVul").append("<b>Heartbeat:</b> " +response.endpoints[0].details.heartbeat+ " (CVE-2014-0160) (<a href='"+CVE0160+"' target='_new'>more info)</a><br>");
                $("#ajaxResponseVul").append("<b>Heartbleed:</b> " +response.endpoints[0].details.heartbleed+ " (CVE-2014-0160) (<a href='"+CVE0160+"' target='_new'>more info</a>)<br>");
                $("#ajaxResponseVul").append("<b>OpenSSL CCS:</b> " +response.endpoints[0].details.heartbleed+ " (CVE-2014-0224) (<a href='"+CVE0224+"' target='_new'>more info</a>)<br>");
                // Vulnerabilities response endpoint 1
                $("#ajaxResponseVul1").append("<b>Supports RC4:</b> " +response.endpoints[1].details.supportsRc4+ "<br>");
                $("#ajaxResponseVul1").append("<b>Beast:</b> " +response.endpoints[1].details.vulnBeast+ " (CVE-2011-3389) (<a href='"+CVE3389+"' target='_new'>more info</a>)<br>");
                $("#ajaxResponseVul1").append("<b>Heartbeat:</b> " +response.endpoints[1].details.heartbeat+ " (CVE-2014-0160) (<a href='"+CVE0160+"' target='_new'>more info)</a><br>");
                $("#ajaxResponseVul1").append("<b>Heartbleed:</b> " +response.endpoints[1].details.heartbleed+ " (CVE-2014-0160) (<a href='"+CVE0160+"' target='_new'>more info</a>)<br>");
                $("#ajaxResponseVul1").append("<b>OpenSSL CCS:</b> " +response.endpoints[1].details.heartbleed+ " (CVE-2014-0224) (<a href='"+CVE0224+"' target='_new'>more info</a>)<br>");
                // Vulnerabilities response endpoint 2
                $("#ajaxResponseVul2").append("<b>Supports RC4:</b> " +response.endpoints[2].details.supportsRc4+ "<br>");
                $("#ajaxResponseVul2").append("<b>Beast:</b> " +response.endpoints[2].details.vulnBeast+ " (CVE-2011-3389) (<a href='"+CVE3389+"' target='_new'>more info</a>)<br>");
                $("#ajaxResponseVul2").append("<b>Heartbeat:</b> " +response.endpoints[2].details.heartbeat+ " (CVE-2014-0160) (<a href='"+CVE0160+"' target='_new'>more info)</a><br>");
                $("#ajaxResponseVul2").append("<b>Heartbleed:</b> " +response.endpoints[2].details.heartbleed+ " (CVE-2014-0160) (<a href='"+CVE0160+"' target='_new'>more info</a>)<br>");
                $("#ajaxResponseVul2").append("<b>OpenSSL CCS:</b> " +response.endpoints[2].details.heartbleed+ " (CVE-2014-0224) (<a href='"+CVE0224+"' target='_new'>more info</a>)<br>");

例如,回调将始终包含至少 response.endpoints[0] 的数据...但根据回调,可能有也可能没有 response.endpoints[1] 的数据。 ... response.endpoints[2]...等

如果您需要我进一步说明,请告诉我..

-- 更新 3 --

已根据 Rory 的指导使用一个循环进行修复。只需要正确考虑这一点。现在使用以下代码一切正常:

                    for (var i = 0; i < response.endpoints.length; i++) {
                var endpoint1 = response.endpoints[i];
                $response0.append("<h3>Endpoint ["+i+"] <font color='green'>&#10004;</font></h3><b>Server Name:</b> " + endpoint1.serverName+ "<br>");
                $response0.append("<b>IP Address:</b> " + endpoint1.ipAddress+ "<br>");
                $response0.append("<b>Grade:</b> " + endpoint1.grade+ "<br>");
                }

您可以检查 javascript 变量是否未定义

例如:

if(response.endpoints[1]==undefined)
{
  //your code
}

if(response.endpoints[2]==undefined)
{
  //your code
}

另一种方法是循环播放

for(i in response.endpoints)
{
  if(i==1)
  {
    //your code with response.endpoints[i]
  }
  if(i==2)
  {
    //your code with response.endpoints[i]
  }
  if(i==3)
  {
    //your code with response.endpoints[i]
  }
}

第二种方式是动态的,但您已经为每个循环设置了条件。

您需要使用循环遍历实际返回的数据。试试这个:

success: function (response) {
    var status = response.status;
    var CVE3389 = "https://community.qualys.com/blogs/securitylabs/2013/09/10/is-beast-still-a-threat?_ga=1.235863681.1412228171.1426286790";
    var CVE0160 = "https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-0160";
    var CVE0224 = "https://community.qualys.com/blogs/securitylabs/2014/04/08/ssl-labs-test-for-the-heartbleed-attack?_ga=1.235863681.1412228171.1426286790";

    if ((status == 'READY' && response.endpoints[0].statusMessage == 'Ready')) {
        $("#ajaxResponsePending").empty();
        var $vul = $("#ajaxResponseVul");
        var $response = $("#ajaxResponse");

        // Host details
        $response.append("<b>Status:</b> " + response.endpoints[0].statusMessage + " ["+response.endpoints[0].progress+"%]<br>");
        $response.append("<b>Host:</b> " + response.host + ":" + response.port + "<br>");
        $response.append("<b>Server:</b> " + response.endpoints[0].details.serverSignature + "<br>");

        for (var i = 0; i < response.endpoints.length; i++) {
            var endpoint = response.endpoints[i];
            $vul.append("<b>Supports RC4:</b> " + endpoint.details.supportsRc4 + "<br>");
            $vul.append("<b>Beast:</b> " + endpoint.details.vulnBeast + " (CVE-2011-3389) (<a href='" + CVE3389 + "' target='_new'>more info</a>)<br>");
            $vul.append("<b>Heartbeat:</b> " + endpoint.details.heartbeat + " (CVE-2014-0160) (<a href='" + CVE0160 + "' target='_new'>more info)</a><br>");
            $vul.append("<b>Heartbleed:</b> " + endpoint.details.heartbleed + " (CVE-2014-0160) (<a href='" + CVE0160 + "' target='_new'>more info</a>)<br>");
            $vul.append("<b>OpenSSL CCS:</b> " + endpoint.details.heartbleed + " (CVE-2014-0224) (<a href='" + CVE0224 + "' target='_new'>more info</a>)<br>");
        }
    }
}