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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!