dongquanyu5816
dongquanyu5816
2013-07-18 23:32

在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 doucan8049 8年前

    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));
    
    点赞 评论 复制链接分享
  • duanpao4522 duanpao4522 8年前

    Just call parse and not stringify JSON.parse(yourData);

    点赞 评论 复制链接分享