Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery PhantomJs脚本中的Ajax请求_Jquery_Ajax_Phantomjs_Local - Fatal编程技术网

Jquery PhantomJs脚本中的Ajax请求

Jquery PhantomJs脚本中的Ajax请求,jquery,ajax,phantomjs,local,Jquery,Ajax,Phantomjs,Local,问题:phantomJs脚本中对本地页面的Ajax请求不起作用(无响应) 问题:我如何才能让它工作?有什么想法或可能的解决方案吗 说明:我正在运行一个phantomJs脚本,需要访问另一个页面(本地)中php函数提供的一些数据。为了做到这一点,我在phantomjs脚本中使用了对该页面的ajax请求。但是,请求没有做任何事情。剧本是: page.open(url, function (status) { page.includeJs('http://ajax.googleapis.com

问题:phantomJs脚本中对本地页面的Ajax请求不起作用(无响应)

问题:我如何才能让它工作?有什么想法或可能的解决方案吗

说明:我正在运行一个phantomJs脚本,需要访问另一个页面(本地)中php函数提供的一些数据。为了做到这一点,我在phantomjs脚本中使用了对该页面的ajax请求。但是,请求没有做任何事情。剧本是:

page.open(url, function (status) {
    page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js', function () {
        console.log("Solve Captcha");
        $.ajax({
            url: 'captcha.php',
            data: { filename: 'C:\\wamp\\www\\images\\0.png' },
            type: 'post',
            success: function (output) {
                console.log('Solved');
                phantom.exit();
            },
        });
    });
});
php页面位于本地WAMP服务器中,并且已经使用ajax(在phantomJs脚本之外)进行了测试,效果良好。脚本和php文件位于文件夹
C:\wamp\www
中,而图像
0.png
位于子文件夹
C:\wamp\www\images

重要提示:页面
captcha.php
位于本地主机中,而phantomJs请求的页面不是本地的,即
页面。open
打开一个非本地的
url

我不明白为什么在phantomJs脚本中发出这个请求不起作用。您能帮助我吗?

page.includeJs()
将jQuery注入页面,因此只能从页面上下文(在
page.evaluate()的内部)访问它。页面上下文是沙盒,因此无法从页面上下文调用
phantom.exit()
,因为没有这样的对象
window.phantom

你有两种可能使它工作

阻塞AJAX
jQuery.ajax()
接受一个
async:false
属性来进行阻塞ajax调用,因此您可以简单地进行调用,然后以迭代方式继续执行

page.open(url, function (status) {
    page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js', function () {
        console.log("Solve Captcha");
        page.evaluate(function(){
            $.ajax({
                async: false, // this
                url: 'http://localhost/captcha.php',
                data: { filename: 'C:\\wamp\\www\\images\\0.png' },
                type: 'post',
                success: function (output) {
                    console.log('Solved');
                },
            });
        });
        phantom.exit();
    });
});
等待完成 从示例中可以使用等待设置特定条件。此条件应在AJAX调用的
success
回调中设置:

page.open(url, function (status) {
    page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js', function () {
        console.log("Solve Captcha");
        page.evaluate(function(){
            window._finishedCall = false;
            $.ajax({
                url: 'http://localhost/captcha.php',
                data: { filename: 'C:\\wamp\\www\\images\\0.png' },
                type: 'post',
                success: function (output) {
                    console.log('Solved');
                    window._finishedCall = true;
                },
            });
        });
        waitFor(function check(){
            return page.evaluate(function(){
                return window._finishedCall;
            });
        }, function onReady(){
            phantom.exit();
        }, 10000); // 10 seconds maximum timeout
    });
});


第二个问题是您希望发出跨域请求,因为
captcha.php
位于localhost上,并且
url
与localhost不同。您需要使用
--web security=false
选项运行PhantomJS,并使用完全限定的URL:
http://localhost/captcha.php

第一个解决方案没有任何问题(
async:false
)。我认为
page.evaluate
实际上无法访问
captcha.php
0.png
。请注册到、、事件。可能有错误。没有错误,“解决验证码”打印在控制台上,并保持在那里。如果在解决方案中包含类似的
phantom.exit()
,则在打印“解决验证码”后,代码立即存在。我记录了函数的输出,得到了一个
null
。也许你应该注册ajax
error
回调,看看请求是否有问题。所以我删除了
async
属性,添加了一个error函数并打印了消息。我收到
无法加载资源(#16URL:**页面的url**/captcha.php)错误代码:203。描述:下载**页面的**url**/captcha.php时出错-服务器回复:未找到
。页面的url是我用
page.open
打开的url,而
$。ajax
试图在该url中加载
captcha.php
,而不是在本地服务器中。