Ajax request in PhantomJs script

Problem: Ajax request in phantomJs script to local page not working (no response)

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

Description: I am running a phantomJs script, and I need to access some data provided by the php function on another page (local). For this, I use the ajax request to this page inside the phantomjs script. However, the request does nothing. script:

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

The php page is located on the local WAMP server and it has been tested with ajax (outside the phantomJs script) and it works great. The script and php files are in the folder C:\wamp\www, and the image 0.pngis in a subfolder C:\wamp\www\images.

: captcha.php , phantomJs , page.open url .

, phantomJs script . ?

+4
1

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

waitFor . success 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 localhost, url localhost. PhantomJS --web-security=false URL-: http://localhost/captcha.php.

+6

All Articles