【问题标题】:Get POST data as object in phantomjs?在phantomjs中获取POST数据作为对象?
【发布时间】:2013-07-18 03:05:28
【问题描述】:

我在this issue 上做了一些阅读,我发现在某一时刻,我想要的都是可能的。在评论 #3 中,这显示了:

request.post = {
  Name : "Jonathan Doe",
  Age : "23",
  Formula : "a + b == 13%!"
}

现在,当我向我的 PhantomJS 网络服务器发送 POST 请求时,这正是我想要得到的。

我是这样发送的:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "localhost:8585");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
 'ajaxUrl' => $ajaxUrl,
 'analysisFile' => $analysisFile,
 'businessId' => $businessId,
 'website' => $website
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); 

..但我得到的请求看起来像这样(JSON.stringify 之后):

{ "headers" : { "Accept" : "*/*",
      "Content-Length" : "555",
      "Content-Type" : "multipart/form-data; boundary=----------------------------ad33c9f28b99",
      "Expect" : "100-continue",
      "Host" : "localhost:8585"
    },
  "httpVersion" : "1.1",
  "method" : "POST",
  "post" : "------------------------------ad33c9f28b99\r\nContent-Disposition: form-data; name=\"ajaxUrl\"\r\n\r\nhttp://localhost/website/ajax.php\r\n------------------------------ad33c9f28b99\r\nContent-Disposition: form-data; name=\"analysisFile\"\r\n\r\nC:\\xampp\\htdocs\\website\\phantom\\get_site_info.js\r\n------------------------------ad33c9f28b99\r\nContent-Disposition: form-data; name=\"businessId\"\r\n\r\n67\r\n------------------------------d33c9f28b99\r\nContent-Disposition: form-data; name=\"website\"\r\n\r\nhttp://www.website.com/\r\n------------------------------ad33c9f28b99--\r\n",
  "url" : "/"
}

如您所见,没有 POST 对象,只是一个包含所有 POST 数据的大字符串。是我通过 cURL 发送它的方式吗?我对此非常陌生,以及我从here 获得的 cURL 代码。

如果有帮助,我正在使用 casperjs 1.1.0-DEV 运行 phantomjs 1.9.1。

【问题讨论】:

    标签: php javascript curl phantomjs


    【解决方案1】:

    只调用 parse 而不是 stringify JSON.parse(yourData);

    【讨论】:

      【解决方案2】:

      除了 Sam Aleksovs 的回答(使用 JSON.parse 而不是 JSON.stringify)。

      您需要将数据编码为 JSON。你不能只发送数组:

      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
      

      【讨论】:

      • 啊哈!我们去吧。对数据进行编码就可以了。奇怪的是,JSON.parse 冻结了 phantoms 网络服务器,所以我不知道,但这只是为了调试。谢谢!
      猜你喜欢
      • 2018-02-15
      • 1970-01-01
      • 2021-10-12
      • 2011-07-17
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多