2017-03-26 32 views
0

我遇到的問題是,我似乎無法使用從請求調用的PHP文件中傳回的原始JS文件的結果。這是結果和處理程序:無法使用來自AJAX請求的字符串結果

if(creds_verification() == true){ 
     $.ajax({ 
     url:"scripts/scripts.php", //the page containing php script 
     type: "post", //request type, 
     dataType: 'json', 
     data: {url: URL, user: USER, password: PASS}, 
     success:function(result){ 
      var verdict = result 
      if(verdict == "Yes"){ 
       Call another external function 
      }else{ 
      console.log(results.abc) 
     } 
     } 
    }); 
    } 

控制檯不斷地打印「是」,又名的results.abc結果......爲什麼會出現這種情況?它應該是在執行下一個函數......

新增信息 PHP腳本運行shell命令和echo荷蘭國際集團的結果:

scripts.php

<?php 
$URL = $_POST['url']; 
$USER = $_POST['user']; 
$PASSWORD = $_POST['password']; 

echo json_encode(array("abc" => shell_exec("PATH.../phantomjs PATH/phantom/examples/test.js 2>&1 $URL $USER $PASSWORD"))); 
?> 

而且JS文件這是來自shell命令的調用:

test.js

var system = require('system') 
var page = require('webpage').create() 
var script = document.createElement('script'); 
script.src = 'http://code.jquery.com/jquery-1.11.0.min.js'; 
script.type = 'text/javascript'; 
document.getElementsByTagName('head')[0].appendChild(script); 
var URL = system.args[1] 
var USER = system.args[2] 
var PASS = system.args[3] 
var steps = [ 
    function() { 
    page.open('http://'+URL+'/login.html') 
    }, 
    function() { 
    page.evaluate(function(USER, PASS) { 
     document.querySelector('input[name="log"]').value = USER 
    document.querySelector('input[name="pwd"]').value = PASS 
    document.querySelector('form').submit() 
    }, USER, PASS) 
    }, 
function(){ 
    var newURL = page.url 
    if(newURL == 'http://'+URL+'/user.html'){ 
     console.log('Yes') 
    }else{ 
     console.log('No?') 
    } 
} 
] 
var stepindex = 0 
var loading = false 

setInterval(executeRequestsStepByStep, 5000) 

function executeRequestsStepByStep(){ 
    if (loading == false && steps[stepindex]) { 
    steps[stepindex]() 
    stepindex++ 
    } 
    if (!steps[stepindex]) { 
    phantom.exit() 
    } 
} 
page.onLoadStarted = function() { loading = true } 
page.onLoadFinished = function() { loading = false } 
+0

沒有足夠的細節 –

+0

可能是這樣的:v erdict ==「是」,不計算爲true,您的函數不會被調用。記錄結果。 –

+0

你確定'verdict'等於'result' ?,看起來你正在覆蓋內容。 –

回答

1

那是因爲你調用服務器上的代碼(這意味着scripts/scripts.php的結果),而不是返回「是」的結果在a abc屬性把「是」返回的對象。

更改PHP代碼:

<?php 
$URL = $_POST['url']; 
$USER = $_POST['user']; 
$PASSWORD = $_POST['password']; 

$res = shell_exec("PATH.../phantomjs PATH/phantom/examples/test.js 2>&1 $URL $USER $PASSWORD"); 
echo json_encode($res == 'Yes' ? $res : array("abc"=>$res)); 
?> 

編輯如果test.js結果就是 「是」 或 「否」,那麼PHP改爲

<?php 
$URL = $_POST['url']; 
$USER = $_POST['user']; 
$PASSWORD = $_POST['password']; 

$res = shell_exec("PATH.../phantomjs PATH/phantom/examples/test.js 2>&1 $URL $USER $PASSWORD"); 
echo json_encode(preg_replace("/[^A-Za-z]/", '', $res)); 
?> 

和JavaScript來

if(creds_verification() == true){ 
     $.ajax({ 
     url:"scripts/scripts.php", //the page containing php script 
     type: "post", //request type, 
     dataType: 'json', 
     data: {url: URL, user: USER, password: PASS}, 
     success:function(result){ 
      if(result == "Yes"){ 
       Call another external function 
      }else{ 
       console.log(result) 
     } 
     } 
    }); 
    } 
+0

我做了這些修改,我也嘗試了'if(result.abc ==「是」)',但都沒有工作。 – Ethan

+0

然後請發佈php代碼。 –

+0

難道這總是會回來嗎?它應該返回任何'test.js'確定(「是」或「否」(「否?」在上面的腳本中)) – Ethan