[英]POST in node.js fails
我正在使用Python API处理对MongoDB的REST调用。 该API基于Kule (因此包括Bottle和PyMongo模块)。 前端是使用node.js构建的。 我一直在使用不同端口的API,前端和MongoDB在localhost上进行开发和测试。
我已经设置了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");
“内容类型”应为大写“ T”的“内容类型”。
愚蠢的错误。 留给别人做笔记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.