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

时间:2015-04-08 10:00:21

标签: php jquery api

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

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

在Web控制台中找到以下错误

  

TypeError:response.endpoints [1]未定义

有很多变量..有什么办法可以用“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>");
                }

2 个答案:

答案 0 :(得分:1)

您需要使用循环来迭代实际返回的数据。试试这个:

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>");
        }
    }
}

答案 1 :(得分:0)

您可以在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]
  }
}

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