如何在CasperJS中进行POST请求后获取响应 [英] How to get the response after a POST request in CasperJS

查看:84
本文介绍了如何在CasperJS中进行POST请求后获取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个非常简单的代码,可以在发布请求后从服务器端点读取响应.实际上,我正在将数据保存到数据库中,并等待响应,然后再进行下一步

I have this very simple code to read the response from a server endpoint after a post request. Actually I'm saving a data to a database and wait for a response before going to next step

casper.open('http://example.com/ajax.php, {
    method: 'POST',
    data: {
        'title': '<title>',
        'unique_id': '<unique_id>'
    }
});

在ajax.php文件上,我试图以一种简单的方式回显POST请求. 如果我从服务器获得正确的响应,这将很容易让我知道.

on ajax.php file I'm trying to echo the POST request in a simple way. this will let me know easily if I'm getting the right response from the server.

echo json_encode($_POST);

我尝试了这些代码段,但无法获得响应.

I tried these snippets but I'm unable to get the response.

casper.on('page.resource.received', function(resp){
    this.echo(JSON.stringify(resp, null, 4));
});

casper.on('http.status.200', function(resp){
    this.echo(JSON.stringify(resp, null, 4));
});

casper.on('resource.received', function(resp) {
    this.echo(JSON.stringify(resp, null, 4));
});

推荐答案

我也遇到了同样的问题,将查询发布到ElasticSearch,但我无法检索结果.

I've been facing the same problem POSTing a query to ElasticSearch and I could not retrieve the results.

据我了解,如果要检索脚本回显的数据,解决方案可能是这样:

As far as I can understand if you want to retrieve the data echoed by your script the solution could be this:

this.echo(this.page.content);

this.echo(this.page.plainText);

在您的功能中.

例如(我的ElasticSearch案例):

For example (my case with ElasticSearch):

/*
 * SOME VAR DEFINITIONS HERE    
 */

casper.start();

casper.then( function() {
    // the next var is very specific to ElasticSearch
    var elasticQuery = JSON.stringify (
      {
        'size' : 20,
        'query' : {
          'filtered' : {
            'filter' : { 'term' : { 'locked' : false } }
          }
        },
        'sort': { 'lastScrapeTime': { 'order': 'asc' } }
      }
    );

    var elasticRequest = {
      method: 'POST',
      data: elasticQuery
    }

    this.thenOpen( <<YOUR URL>>, elasticRequest, function (response) {
      // dump response header
      require('utils').dump(response);

      // echo response body
      this.echo(this.page.content);

      // echo response body with no tags added (useful for JSON)
      this.echo(this.page.plainText);
    }); 
  }
);

casper.run();

这篇关于如何在CasperJS中进行POST请求后获取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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