在PhantomJs脚本Ajax请求 [英] Ajax request in PhantomJs script

查看:2523
本文介绍了在PhantomJs脚本Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:阿贾克斯内phantomJs脚本要求当地页面无法正常工作(不响应)

Problem: Ajax request within phantomJs script to a local page doesn't work (no response)

问:我怎样才能使它发挥作用?任何意见或可能的解决方案?

Question: How can I make it work? Any ideas or possible solutions?

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

Description: I'm running a phantomJs script and I need to access some data that is provided by a php function in another page (local). In order to do that, I use an ajax request to that page inside the phantomjs script. However, the request doesn't do anything. The script is:

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的服务器,它已经过测试与阿贾克斯(一phantomJs脚本之外),并能正常工作。脚本和PHP文件夹中 C:\瓦帕\的www ,而图像 0.png 是在子文件夹 C:\瓦帕\的www \图片

The php page is in a local WAMP server and it has been tested with ajax (outside a phantomJs script), and it works fine. The script and the php files are in the folder C:\wamp\www, while the image 0.png is in the sub-folder C:\wamp\www\images.

重要提示:页面 captcha.php 是本地主机,而phantomJs请求一个网页,是的不可以地方,也就是 page.open 打开网​​址不是本地的。

Important: The page captcha.php is in the localhost, while phantomJs is requesting a page that is not local, that is, page.open opens a url that is not local.

我不明白,为什么提出这个要求在phantomJs脚本将无法正常工作。你能帮帮我吗?

I don't understand why making this request in a phantomJs script won't work. Could you please help me?

推荐答案

page.includeJs()的jQuery注入到页面,所以它是从页面上下文中唯一的访问(内 page.evaluate())。该页面上下文是沙箱,所以你不能叫 phantom.exit()从页面的背景下,因为没有这样的对象 window.phantom

page.includeJs() injects jQuery into the page, so it is only accessible from the page context (inside of page.evaluate()). The page context is sandboxed, so you cannot call phantom.exit() from the page context, because there is no such object window.phantom.

您有两种可能性,以使其发挥作用。

You have two possibilities to make it work.

jQuery.ajax()接受一个异步:假属性,以使阻塞的AJAX调用,这样你就可以只需拨打电话,然后继续执行迭代的风格。

jQuery.ajax() accepts an async: false property to make a blocking AJAX call, so you can simply make the call and then continue with the execution in iterative style.

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();
    });
});

等待完成

WAITFOR 的从实施例可以用来等待将要设定的特定条件。这种情况应该在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(){
            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 是在本地主机和网​​址是从本地主机截然不同。您需要运行PhantomJS与 - 网络安全=假选项,并使用完全限定网址:的http://localhost/captcha.php


The second problem is that you want to make a cross-domain request, because captcha.php is on localhost and url is distinct from localhost. You need to run PhantomJS with the --web-security=false option and use fully qualified URLs: http://localhost/captcha.php.

这篇关于在PhantomJs脚本Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆