PhantomJS GET URL参数

时间:2014-11-13 23:34:40

标签: javascript phantomjs

如何在PhantomJS下运行服务器时检索GET URL参数。这是代码。

var webserver = require('webserver');

var server = webserver.create();

var service = server.listen(9090, function(request, response)
{
  var page = require('webpage').create();

  console.log('GET: ' + request.get)
  console.log('POST: ' + request.post)

1 个答案:

答案 0 :(得分:3)

Web服务器模块不像PHP那样为您解析参数。你需要自己做。

server.listen(9090, function(request, response) {
  // parse url property to get the GET parameters
  console.log('URL: ' + request.url);
  console.log("    " + JSON.stringify(parseGET(request.url), undefined, 4)); // pretty print

  // parse post property to get the POST parameters (message body)
  console.log('BODY: ' + request.post);
};

function parseGET(url){
  // adapted from http://stackoverflow.com/a/8486188
  var query = url.substr(url.indexOf("?")+1);
  var result = {};
  query.split("&").forEach(function(part) {
    var e = part.indexOf("=")
    var key = part.substr(0, e);
    var value = part.substr(e+1);
    result[key] = decodeURIComponent(value);
  });
  return result;
}

可以找到完整的文档here