我遇到的问题是我似乎无法使用从请求调用的JS
文件中传回原始PHP
文件的结果。这是结果和处理程序:
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
的结果...为什么会发生这种情况?它应该执行下一个功能...
添加了信息
运行shell命令的PHP
脚本和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")));
?>
这是从shell命令调用的JS
文件:
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 :(得分:1)
这是因为您在服务器上调用的代码(这意味着scripts/scripts.php
的结果)而不是返回&#39;是&#39;因此结果是“是”&#39;在返回对象的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
的结果只是&#34;是&#34; 修改或&#34;否&#34;然后将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)
}
}
});
}