nodejs 如何处理POST的数据

robinclark 发布于 2015/01/09 15:44
阅读 5K+
收藏 0

这里是JavaScript请求:

post1.addEventListener('click', function(){

      console.log("start to post a message to server");
      request.open("POST", "http://127.0.0.1:12345");
      request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
      var data = {
         name: "atekul",
      };

      request.onreadystatechange = function(){
         if(request.readyState === 4 && request.status === 200){
            console.log("success");
            showarea.innerHTML = request.responseText;
            showerror.style.visibility = "hidden";
         }
         else{
            showerror.style.visibility = "visible";
         }
      };
      request.send(data);

   },false);



下面是Node端:

if(request.method === 'POST') {

      console.log("POST");
      var body = '';
      request.on('data', function (data) {
         body += data;
         console.log("Partial body: " + body);
      });
      request.on('end', function () {
         console.log("Body: " + body);
      });
      response.writeHead(200, {'Content-Type': 'text/html'});
      response.end('post received');

   }



在Node里面如何获取这个提交过来的data对象里面的属性和值呢?我node输出的body对象显示是一个[Object object]

加载中
返回顶部
顶部