dongquanyu5816 2013-07-18 23:32
浏览 145
已采纳

在phantomjs中获取POST数据作为对象?

I did some reading on this issue and I found that, at one point, what I want was possible. In comment #3, this shows up:

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

Now, this is exactly what I want to get when I send a POST request to my PhantomJS webserver.

I'm sending it like this:

$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); 

..but the request I get just looks like this (after 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
Content-Disposition: form-data; name=\"ajaxUrl\"

http://localhost/website/ajax.php
------------------------------ad33c9f28b99
Content-Disposition: form-data; name=\"analysisFile\"

C:\\xampp\\htdocs\\website\\phantom\\get_site_info.js
------------------------------ad33c9f28b99
Content-Disposition: form-data; name=\"businessId\"

67
------------------------------d33c9f28b99
Content-Disposition: form-data; name=\"website\"

http://www.website.com/
------------------------------ad33c9f28b99--
",
  "url" : "/"
}

As you can see, there's no POST object, just a large string that has all of the POST data in it. Is it the way I'm sending it via cURL? I'm pretty unfamiliar with that, and the cURL code I got from here.

I'm running phantomjs 1.9.1 with casperjs 1.1.0-DEV, if that helps.

  • 写回答

2条回答 默认 最新

  • doucan8049 2013-07-18 23:41
    关注

    In addition to Sam Aleksovs answer (Use JSON.parse not JSON.stringify).

    You need to encode the data to JSON. You can't just send the array:

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?