php变量使用getJSON()

时间:2017-08-17 06:10:12

标签: javascript php

我想将php变量传递给javascript

在php(getStatus.php)上我正在使用像这样的json_encode

$resultFin = array();
if (strpos($status,'Scan is complete') === true){
$resultFin[] = 1;
}
else{ 
    $resultFin[] = 0;
}

echo json_encode($resultFin);
echo "$status";

我希望在javascript上获得resultFin值,所以我写下面的代码

echo '<script type="text/javascript">
$(document).ready(function() {
$.post("scanner/getStatus.php", {testId:' . "$testId" . ',chkCnt:' . "$chkCount" . ',rxss:' . "$rxss" . ',sxss:' . "$sxss" . ',sqli:' . "$sqli" . ',basqli:' . "$basqli" . ',autoc:' . "$autoc" . ',idor:' . "$idor" . ',dirlist:' . "$dirlist" . ',bannerdis:' . "$bannerdis" . ',sslcert:' . "$sslcert" . ',unredir:' . "$unredir" . ',clamav:' . "$clamav" . '}, function(data){$("#status").html(data)});
     var refreshId = setInterval(function() {
$.post("scanner/getStatus.php", {testId:' . "$testId" . ',chkCnt:' . "$chkCount" . ',rxss:' . "$rxss" . ',sxss:' . "$sxss" . ',sqli:' . "$sqli" . ',basqli:' . "$basqli" . ',autoc:' . "$autoc" . ',idor:' . "$idor" . ',dirlist:' . "$dirlist" . ',bannerdis:' . "$bannerdis" . ',sslcert:' . "$sslcert" . ',unredir:' . "$unredir" . ',clamav:' . "$clamav" . '}, function(data){$("#status").html(data)});
console.log("dfdfdf");
$.getJSON("scanner/getStatus.php", function(data){
        var fini;
        fini = data;
        console.log(fini);
        if( fini == 1){
            clearInterval(refreshId);
            }
        });
}, 500);
$.ajaxSetup({ cache: false });
});</script>';

我使用brakepoint console.log检查了它(&#34; dfdfd&#34;);

但没有进入getJSON方法而且没有获得resultFin值

如何修复此代码?

我想在javascript中使用resultFin值

1 个答案:

答案 0 :(得分:1)

你的getStatus.php应如下所示:

$resultFin = array();
if (strpos($status,'Scan is complete') === true){
$resultFin['scan_completed'] = true;
}
else{ 
    $resultFin['scan_completed'] = false;
}

echo json_encode($resultFin);

所以在前端你可以使用fini.scan_completed,这将是真或假。