Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX错误:400个错误请求_Javascript_Ajax_Node.js_Express_Mithril.js - Fatal编程技术网

Javascript AJAX错误:400个错误请求

Javascript AJAX错误:400个错误请求,javascript,ajax,node.js,express,mithril.js,Javascript,Ajax,Node.js,Express,Mithril.js,我正在使用mithril.js连接我的节点后端。我一直在关注添加到AJAX请求中的文档,其他地方几乎没有关于mithril的文档 无论如何,错误: mithril.js:2130 POST http://localhost:3000/api/stocks 400 (Bad Request) ta @ mithril.js:2130 ua @ mithril.js:2138 k.request @ mithril.js:2227 vm.add @ app.js:24 (anonymous) @ m

我正在使用mithril.js连接我的节点后端。我一直在关注添加到AJAX请求中的文档,其他地方几乎没有关于mithril的文档

无论如何,错误:

mithril.js:2130 POST http://localhost:3000/api/stocks 400 (Bad Request)
ta @ mithril.js:2130
ua @ mithril.js:2138
k.request @ mithril.js:2227
vm.add @ app.js:24
(anonymous) @ mithril.js:1246
mithril.js:2197 Uncaught SyntaxError: Unexpected token < in JSON at position 0
    at parse (<anonymous>)
    at Object.a.onload.a.onerror (mithril.js:2197)
    at XMLHttpRequest.d.onreadystatechange (mithril.js:2102)
a.onload.a.onerror @ mithril.js:2197
d.onreadystatechange @ mithril.js:2102
routes/index.js节点

router.post('/api/stocks', function(req, res) {
  Stocks.create({
    stock: req.body.text, //stocks
    date_added: new Date(), //Date
  }, function (err, stocks) {
    if (err) {
      res.send(err);
    } else {
      Stocks.find(function(err, stocks) {
        if (err) {
          res.send(err);
        } else {
          res.json(stocks);
        }
      });
    };
  });
});
有什么想法吗


谢谢。

这个问题是因为Mithril自动假定数据是JSON格式的。我正试图发送纯文本。Mithril为此提供了一个称为反序列化的方法


反序列化不起作用,所以我再次查看,express不喜欢没有JSON的事实,所以我将数据转换为JSON,然后POST请求工作正常

你能把你的有效载荷寄到这里吗?
router.post('/api/stocks', function(req, res) {
  Stocks.create({
    stock: req.body.text, //stocks
    date_added: new Date(), //Date
  }, function (err, stocks) {
    if (err) {
      res.send(err);
    } else {
      Stocks.find(function(err, stocks) {
        if (err) {
          res.send(err);
        } else {
          res.json(stocks);
        }
      });
    };
  });
});