【发布时间】:2017-04-23 18:59:56
【问题描述】:
我正在尝试在我的 Node 应用程序中发出发布请求;但是,我收到以下错误。
OPTIONS http://localhost:27017/postDebate net::ERR_EMPTY_RESPONSE
如何解决这个问题?
这是我的路线:
var express = require('express');
var router = express.Router();
var Debate = require('../models/debate');
var mdb = require('mongodb').MongoClient,
ObjectId = require('mongodb').ObjectID,
assert = require('assert');
var api_version = '1';
var url = 'mongodb://localhost:27017/debate';
router.post('/'+api_version+'/postDebate', function(req, res, next) {
var debate = new Debate(req.body);
console.log(debate, "here is the debate");
debate.save(function(err) {
if (err) throw err;
console.log('Debate saved successfully!');
});
res.json(debate);
});
module.exports = router;
由于我在 onclick 调用我的 ejs 文件中的函数后调用此路由,因此这是我的 javascript 文件。
function postDebate() {
var topic = document.getElementById('topic').value;
var tags = document.getElementById('tags').value;
var argument = document.getElementById('argument').value;
var debateObject = {
"topic": topic,
"tags": tags,
"argument": argument
};
console.log(topic, tags, argument);
$.ajax({
type: 'POST',
data: JSON.stringify(debateObject),
contentType: "application/json",
//contentType: "application/x-www-form-urlencoded",
dataType:'json',
url: 'http://localhost:27017/post',
success: function(data) {
console.log(JSON.stringify(data), "This is the debateObject");
},
error: function(error) {
console.log(error);
}
});
}
如何解决此错误?这里有什么问题?
OPTIONS http://localhost:27017/postDebate net::ERR_EMPTY_RESPONSE
【问题讨论】:
-
您解决了这个问题吗?自从我遇到这个问题以来已经有 2 周了,我尝试过的所有方法都不起作用......谢谢!
标签: javascript node.js ajax express post