2017-03-26 31 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