如何获得json参数,如果我在顶点使用post方法

How get json parameters if I use post method in vertx?

本文关键字:顶点 post 方法 json 何获得 参数 如果      更新时间:2023-09-26

我想获得所有的post参数。
这是我的顶点代码:

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

我知道怎么做了

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