如果我在vertx中使用post方法,如何获取json参数?

时间:2014-06-17 21:53:57

标签: javascript jquery ajax vert.x

我想获得所有帖子参数 这是我的vertx代码:

    var vertx = require('vertx/http');
var console = require('vertx/console');

var Server= vertx.createHttpServer();
    Server.requestHandler(function(req) {   
    console.log(req.path());    
        console.log(req.method());  
    console.log(req.params());
    // nothing
        if(myPostparametersContainAnyitem){ //do anything}
    else{
      var file = req.path() === '/' ? 'index.html' : req.path();    
      req.response.sendFile('html/' + file);}

  }).listen(8081)

这是我的按钮点击代码:

$.ajax({
            data:  {name:'foo',age:'13'},
            url:   '/somedir',
            type:  'post',
            dataType: 'json',            
            success:  function (response) {
                    alert(response);
            }
      });

1 个答案:

答案 0 :(得分:1)

我发现了如何做到这一点,谢谢

      var vertx = require('vertx');
  var console = require('vertx/console');

  var Server = vertx.createHttpServer();

  Server.requestHandler(function (req) {
      var file = req.path() === '/' ? 'index.html' : req.path();
      if (file === '/foo') {
          foo(req);
      }
      else{
       req.response.sendFile('html/' + file);
      }
  }).listen(8081);

  function foo(req) {
     console.log(req);
      req.bodyHandler(function (data) {
          //data is json {name:foo, age:13}   
        //do  
          req.response.end(data);
      });
  }