node.js中的POST失败

POST in node.js fails

本文关键字:失败 POST 中的 js node      更新时间:2023-09-26

我正在研究一个Python API来处理对MongoDB的REST调用。该API基于Kule(因此包括Bottle和PyMongo模块)。前端是使用node.js构建的。我一直在本地主机上开发和测试不同端口上的API,前端和MongoDB。

我已经设置了API,并一直试图让CRUD请求工作。我可以让这些工作从Python脚本以及从Apache服务器上运行的Javascript。但是,当我将相同的代码添加到前端时,GET可以工作,但POST失败并出现500错误。

这个POST代码适用于Apache服务器:

var myrequest=new XMLHttpRequest();
myrequest.onreadystatechange=function(){
  if (myrequest.readyState==4){
    if (myrequest.status==201){
      alert(myrequest.responseText);
    } else{
      alert("An error has occured making the request");
    }
  }
}
myrequest.open("POST", "http://localhost:8080/items", true);
myrequest.setRequestHeader("Content-type", "application/json;charset=UTF-8");
myrequest.send(JSON.stringify({'name' : 'Name', 'description' : 'Description'}));

当我将此添加到前端时,我从Kule获得以下回溯:

Traceback (most recent call last):
  File "C:'Python27'lib'site-packages'bottle.py", line 764, in _handle
    return route.call(**args)
  File "C:'Python27'lib'site-packages'bottle.py", line 1625, in wrapper
    rv = callback(*a, **ka)
  File "C:'Python27'lib'site-packages'bottle.py", line 1575, in wrapper
    rv = callback(*a, **ka)
  File "C:'Python27'lib'site-packages'kule'kule.py", line 64, in post_list
    inserted = collection.insert(request.json)
  File "C:'Python27'lib'site-packages'pymongo'collection.py", line 351, in insert
    docs = [self.__database._fix_incoming(doc, self) for doc in docs]
TypeError: 'NoneType' object is not iterable
127.0.0.1 - - [24/Jun/2014 07:53:40] "POST /scenarios HTTP/1.1" 500 50

那么,我做错了什么?为什么Javascript在node.js中中断?我有GET工作,但POST失败。为什么?

谢谢!

我发现我的错误了。

myrequest.setRequestHeader("Content-type", "application/json;charset=UTF-8");

"Content-type"应为"Content-type"加一个大写"T"。

愚蠢的错误。留作留言条