POST上请求正文始终为空

时间:2016-02-20 07:19:23

标签: javascript ajax node.js post express

客户方面,这就是我发生的事情:

 function saveGrades() {
  $.post("/savegrades", {classIndex: "classIndexId"});  }

服务器端:

router.post('/savegrades', stormpath.loginRequired, function(req, res)  {
  console.log("Class index: " + req.body.classIndex);
  console.log(req.body);
  res.send(200);
});

我的bodyParser设置如下:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

无论我尝试过什么,req.body都是空的,没有classIndex。我做错了什么?为什么数据没有发布到服务器?

编辑:对于链接的问题,我在这里已经完成了几乎所有相关的答案,但无法找到解决方案。似乎数据永远不会被发送到服务器。当我用调试器检查时,正文总是空的。

2 个答案:

答案 0 :(得分:2)

请检查代码,

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

之前实施得很好,

router.post('/savegrades',

根据您的评论,

您可以尝试添加Trying在客户端添加mime类型(内容类型)。

答案 1 :(得分:1)

尝试此操作并通过 POSTMEN 发送数据以验证数据是否实际发送然后通过函数发送

>>> ubt_conll_chunk_accuracy(conll_train, conll_test)
u:

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    ubt_conll_chunk_accuracy(conll_train, conll_test)
  File "<pyshell#7>", line 6, in ubt_conll_chunk_accuracy
    print 'u:', accuracy(u_chunker, test_chunks)
  File "C:\Python27\lib\site-packages\nltk\metrics\scores.py", line 38, in accuracy
    if len(reference) != len(test):
TypeError: object of type 'UnigramTagger' has no len()
>>> treebank_sents = nltk.corpus.treebank_chunk.chunked_sents()
>>> ubt_conll_chunk_accuracy(treebank_sents[:2000], treebank_sents[2000:])
u:

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    ubt_conll_chunk_accuracy(treebank_sents[:2000], treebank_sents[2000:])
  File "<pyshell#7>", line 6, in ubt_conll_chunk_accuracy
    print 'u:', accuracy(u_chunker, test_chunks)
  File "C:\Python27\lib\site-packages\nltk\metrics\scores.py", line 38, in accuracy
    if len(reference) != len(test):
TypeError: object of type 'UnigramTagger' has no len()
>>>