PhantomJs - 获得价值了page.evaluate + Ajax技术(同步版) [英] PhantomJs - Getting value out of page.evaluate + ajax (Synchronous Version)

查看:212
本文介绍了PhantomJs - 获得价值了page.evaluate + Ajax技术(同步版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题: AJAX 请求中提取里面的数据 page.evaluate

Problem: Extracting data from ajax request inside page.evaluate

说明:我通常变量超出 page.evaluate 通过简单地返回它们。不过,我需要做一个页面的上下文中的 AJAX 请求,然后我需要处理它的结果出来的页面的上下文。

Description: I usually get variables out of page.evaluate by simply returning them. However, I need to make an ajax request within the context of a page, and then I need to process its result out of the page's context.

在code我试图解决的是:

The code I'm trying to fix is:

var theOutput = page.evaluate(function () {
    return $.ajax({
        async: false,
        url: 'http://localhost:8080/captcha.php',
        data: { filename: 'C:\\wamp\\www\\images\\0.png' },
        type: 'post',
        success: function (output) {
            parsed_output = $.parseHTML(output);
            return parsed_output[4].data.trim();
        },
    });
});
console.log(theOutput);

变量 parsed_output [4] .data.trim()是一个字符串。但是,当我登录输出我收到了 [对象的对象] ,与属性中止,一如既往,完整的,做的,错误,失败,getAllResponseHeaders,getResponseHeader,overrideMimeType,管道空,进度,承诺,readyState的,setRequestHeader,状态,状态code,成功,那么

The variable parsed_output[4].data.trim() is a string. But when I log output I get a [object Object], with the properties abort, always, complete, done, error, fail, getAllResponseHeaders, getResponseHeader, overrideMimeType, pipe null, progress, promise, readyState, setRequestHeader, state, statusCode, success,then.

问:我怎么能提取物 theOutput page.evaluate

推荐答案

由于这是一个阻挡AJAX请求,您可以创建一个临时变量:

Since this is a blocking AJAX request, you can create a temporary variable:

var theOutput = page.evaluate(function () {
    var result;
    $.ajax({
        async: false,
        ...
        success: function (output) {
            parsed_output = $.parseHTML(output);
            result = parsed_output[4].data.trim();
        },
    });
    return result;
});
console.log(theOutput);

您也可以直接访问的responseText 从jqXHR对象:

You can also directly access the responseText from the jqXHR object:

var theOutput = page.evaluate(function () {
    var jqXHR = $.ajax({
        async: false,
        url: 'http://localhost:8080/captcha.php',
        data: { filename: 'C:\\wamp\\www\\images\\0.png' },
        type: 'post'
    });
    parsed_output = $.parseHTML(jqXHR.responseText);
    return parsed_output[4].data.trim();
});
console.log(theOutput);

如果你担心异步:假是pcated德$ P $,你可以简单地使用底层XMLHtt prequest使用阻塞执行:

If you fear that async: false is deprecated, you can simply use the underlying XMLHttpRequest to use blocking execution:

var theOutput = page.evaluate(function () {
    var request = new XMLHttpRequest();
    request.open('POST', 'http://localhost:8080/captcha.php', false);
    request.send($.param({ filename: 'C:\\wamp\\www\\images\\0.png' }));

    var parsed_output = $.parseHTML(request.responseText);
    return parsed_output[4].data.trim();
});
console.log(theOutput);

这篇关于PhantomJs - 获得价值了page.evaluate + Ajax技术(同步版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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