CasperJS将空POST数据发送到端点 [英] CasperJS sends empty POST data to endpoint

查看:70
本文介绍了CasperJS将空POST数据发送到端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CasperJS来抓取一个站点,构建一个JSON对象来组织所捕获的信息,然后将这样的JSON对象POST到一个ETL信息到数据库的服务(在PHP中)。

I'm using CasperJS to scrape a site, build a JSON object to organize the info scraped, and then POSTing such JSON object to a service (in PHP) that ETLs the info into a database.

这是问题,每当我尝试发送JSON对象时,对象为空,这是我用来发送信息的代码。

Here's the problem, whenever I try to send the JSON Object the, the object is empty, this is the code I'm using to send the info.

casper.then(function(){
    var productDetails = {};
    this.each(products,function(self, product){
        self.thenOpen(product, function(a){
            productDetails = this.evaluate(getProductDetails);
        });
        self.thenOpen('http://localhost.endpoint.lan/saveScrapingData.php', {
            method:'post',
            data: {
                name:'Alan',
                info: productDetails
            },
            headers: {
                'Content-type': 'application/x-www-form-urlencoded'
            }
        });
    });
});

问题是productDetails被发送为空,我不知道为什么因为如果我 console.log 此时的变量:

the problem is that productDetails is being sent as empty, and I don't know why because if I console.log the variable at this point:

self.thenOpen(product, function(a){
    productDetails = this.evaluate(getProductDetails);
});

变量不为空,很可能这是一个竞赛条件。变量在包含任何数据之前发送,但我不明白为什么会发生这种情况,如果我在确定变量包含数据后发送数据。

the variable is not empty, more than likely this is a racing condition. The variable is being sent before it contains any data, but I don't understand why is this happening, if I'm sending the data after I'm sure the variable contains data.

推荐答案

是的,在致电然后打开 productDetails 变量仍未定义。您可以将 thenOpen 调用分为然后打开因此,当实际调用 open 时定义它。

Yes, at the time of calling thenOpen the productDetails variable is still undefined. You can just split the thenOpen call into then and open so that it is defined when open is actually called.

self.then(function(){
    this.open('http://localhost.endpoint.lan/saveScrapingData.php', {
        method:'post',
        data: {
            name:'Alan',
            info: productDetails
        },
        headers: {
            'Content-type': 'application/x-www-form-urlencoded'
        }
    });
});

这是因为casper步骤的异步方式(然后* wait * )。它们已预定但稍后执行。问题是通过使用settings对象调用 thenOpen productDetails 的值修复为 undefined ,因为上一步尚未执行。

This is because of the asynchronous manner of casper steps (then* or wait*). They are scheduled but executed later. The problem is that by calling thenOpen with the settings object fixes the value of productDetails to undefined since the the previous step was not yet executed.

这篇关于CasperJS将空POST数据发送到端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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