Javascript 如何在Express.js中处理POST数据?

Javascript 如何在Express.js中处理POST数据?,javascript,jquery,ajax,json,express,Javascript,Jquery,Ajax,Json,Express,我目前一直从Express.js中收到500个错误,我认为这是由于未能获得请求所依赖的字符串键 在客户端,我有几个请求到达同一点(/restore),所有这些请求都是为了使“key”成为jQuery.ajax()调用的数据字典中的一个字段,依次包含在主字典中。在客户端,我有以下内容,其中包括我认为与此无关的本地存储回退: var restore = function(key, default_value, state, callback) { populate_state(state,

我目前一直从Express.js中收到500个错误,我认为这是由于未能获得请求所依赖的字符串键

在客户端,我有几个请求到达同一点(
/restore
),所有这些请求都是为了使“key”成为jQuery.ajax()调用的
数据
字典中的一个字段,依次包含在主字典中。在客户端,我有以下内容,其中包括我认为与此无关的本地存储回退:

var restore = function(key, default_value, state, callback)
  {
  populate_state(state, default_value);
  state.initialized = false;
  var complete = function(jqxhr)
    {
    if (jqxhr.responseText === 'undefined')
      {
      }
    else
      {
      populate_state(state, JSON.parse(jqxhr.responseText));
      }
    callback();
    state.initialized = true;
    }
  jQuery.ajax('/restore',
    {
    'complete': complete,
    'data':
      {
      'key': key,
      'userid': userid
      },
    'method': 'POST',
    });
  if (Modernizr.localstorage)
    {
    if (localStorage[key] === null || localStorage[key]
      === undefined)
      {
      return default_value;
      }
    else
      {
      return JSON.parse(localStorage[key]);
      }
    }
  else
    {
    return default_value;
    }
  }

  restore('Calendar', default_value,
    default_value, function()
    {
    jQuery('#submit-calendar').prop('disabled', false);
    });

    restore('Scratchpad', '', result, function()
      {
      for(var instance in CKEDITOR.instances)
        {
        if (CKEDITOR.instances[instance])
          {
          CKEDITOR.instances[instance].setReadOnly(false);
          }
        }
      });

    return restore('Todo', {
      'items': [],
      'text': ''
      },
      {
      'items': [],
      'text': ''
      },
      function()
        {
        jQuery('#add-activity-button').prop('disabled', false);
        jQuery('#todo-new-entries').prop('disabled', false);
        });

  return restore('YouPick', {
    start_time: new Date().getTime()
    },
    {
    start_time: new Date().getTime()
    },
    function()
      {
      });
请注意,每次调用restore()都会显式地为键指定一个非空、唯一的字母字符串作为第一个参数

在服务器端,Express的routes/index.js有一个为请求提供服务的视图:

router.post('/restore', function(request, response, next)
  {
  console.log('router.post /restore');
  console.log('Query: ' + request.query);
  console.log('href: ' + sanitize(request.user.href));
  console.log('key: ' + sanitize(request.query.key));
  var result = look_up_key(request.user.href, request.query.key);
  console.log(result);
  response.type('application/json');
  response.send(result);
  });
sanitize函数清除非字母数字或显式枚举标点符号的字符。它不应该对纯字母键有任何请求

对于多个调用,其/bin/www的输出为:

router.post /restore
Query: [object Object]
href: httpsapi.stormpath.comv1accounts**********************
POST /restore 500 39.516 ms - 1210
router.post /restore
Query: [object Object]
href: httpsapi.stormpath.comv1accounts**********************
POST /restore 500 5.000 ms - 1210
router.post /restore
Query: [object Object]
href: httpsapi.stormpath.comv1accounts**********************
POST /restore 500 5.842 ms - 1210
看起来查询中有一些内容,但我在哪里访问它?似乎我应该能够将其视为字典,但在服务器端的console.log()调用中,
console.log('key:'+sanitize(request.query.key))
似乎不会产生任何输出,即使是空的或损坏的密钥。它似乎在那里坠毁,显然从那里发送了500

我可能,或者至少可能,通过将数据编码和解码为JSON来规避这个问题,虽然我认为这通常是一个成功的解决方案,但我想理解为什么这不起作用

我也不认为
是某人的保留字;从
标识符
的全局手动搜索和替换似乎不会明显改变行为

所以有两个问题,按优先顺序:

1:如何发送和接收变量,这些变量将被解释为将内容放入GET或POST查询字符串中,或从同一字符串中取出?request.query所代表的
[Object Object]
是什么

2:如果这不是我要走的路线,我应该只使用JSON,那么在这个确切的上下文中,我应该知道关于JSON编码的什么(如果有的话)?它是否像JSON通常那样简单,或者有什么事情应该告诉我


谢谢,

对于
POST
请求,数据通常在请求正文中传递,这意味着您需要使用
req.body
,而不是
req.query
(后者用于访问URL的查询字符串中传递的数据)。在
req.body
工作之前,您需要包括中间件

至于为什么要记录
[object object]
:这与字符串化有关。您的log命令使用
+
来“添加”字符串和对象(
请求.查询
),这将使用对象的字符串表示形式,它恰好是
[对象对象
]

相反,您应该将对象作为单独的参数传递给
console.log()

这将显示对象的更精细的字符串表示。或者,您甚至可以将其打印为JSON:

console.log('Query: %j', request.query)

您应该记录错误,以了解500发生的确切原因。如果将
request.query
作为单独的参数传递给
console.log()
console.log(
query:',request.query)
),您将看到它很可能是一个空对象,因为对于
POST
请求,您需要使用
request.body
(这也需要使用)。@robertklep谢谢;请在回答中重申您的意见,以便我可以接受。
console.log('Query: %j', request.query)