【发布时间】:2017-12-29 17:11:50
【问题描述】:
我正在尝试根据数据库查询返回文本 - 用于帐户注册 - 在 $.ajax success' 参数上,经过多次搜索,我无法获得下面的代码有什么问题。
我找不到如何发送需要异步功能的http响应,如果我尝试这样做,则根本不会处理或检测到请求。
我认为问题在于我的 res.end("false") 调用没有及时调用,但代码对我来说看起来是正确的。
我不想使用 express 并且所有回调都正常工作但是我确定问题出在 server.js 我放评论的地方
客户端:
$.ajax({
async: true,
dataType: "text",
type: 'POST',
url: 'http://192.168.0.23:3000',
data: JSON.stringify(account_info),
contentType: 'application/json; charset=utf-8',
success: function (res) {
console.log('Account registration : ' + res);
},
complete: function (res) {
console.log('Account registration complete : ' +
JSON.stringify(res));
},
error: function (err) {
console.log(err.responseText)
}
});
服务器端:
server.js
const http = require('http');
var mongoose = require('mongoose');
var Visitor = require('./models/visitor.js');
var Account = require('./models/account.js');
var api = require('./controllers/api.js');
var isExisting = api.isExisting;
var saveData = api.saveData;
const port = 3000;
const server = http.createServer();
console.log('server is listening on ' + port);
server.listen(port);
server.on('request', function (request, response) {
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Methods', 'POST');
response.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
console.log(request.method);
var body = '';
request.on('data', function (data) {
body += data;
});
request.on('end', function () {
//In case there's content in the POST request
if (body) {
console.log('\nRequest content:' + body + '\n');
body = JSON.parse(body);
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/someDB', {
useMongoClient: true
});
//Pattern = ACCOUNT_REGISTRATION
if (body.pattern == 'account_registration') {
var value = {
email: body.email
}
var new_data = new Account(Account.store(body));
//Check if account_name or website URL already in db
// exist returning the callback
isExisting(Account, value, function (exist) {
console.log(exist);
if (!exist) {
saveData(new_data);
//If you dont remove this line, the request is not detected by nodeJS
response.end('true');
} else {
console.log('\nAccount already exist.');
//If you dont remove this line, the request is not detected by nodeJS
response.end('false');
mongoose.connection.close();
}
});
}
}
//Here it's working good but If I remove this line it'll not handle the request at all
response.end('oko');
});
});
api.js
// The API controller
var mongoose = require('mongoose');
//Send some new_data to db
exports.saveData = function (new_data) {
//Data saving into MongoDB database
new_data.save(function (err) {
if (err) {
throw err;
}
console.log('\nData successfully added.');
// We need to disconnect now
mongoose.connection.close();
});
}
exports.isExisting = function (ModelName, value, callback) {
ModelName.count(value, function (err, count) {
if (err)
throw err;
if (count == 0)
callback(false);
else
callback(true);
});
}
最后编辑:简而言之,
这是我不删除最后一行时得到的结果(正常行为但我无法获得异步响应
server is listening on 3000
OPTIONS
POST Request content:{"*****"}//real data already in db
true //This is isExisting() callback
Account already exist.
但是当我删除最后一个 response.end('oko') 时,OPTIONS 之后的所有内容都不会出现...
【问题讨论】:
-
if (!exist) { saveData(new_data); }如果没有现有帐户,您会将响应发送到哪里?不在 saveData() 中。我假设它是由您要删除的response.end( 'oko' )行发送的,所以您尝试过if (!exist) { saveData(new_data); response.end( 'oko' ); } -
我猜你的编辑解释了它。
-
如果我删除 response.end('oko');该请求不再处理,但是,使用上面的代码,我在 isExist() 附近进行的所有 response.end 调用都不会执行,即使我删除了最后一个......
-
你找到它停止的地方了吗?您是否仍然看到
console.log(exist);您是否还有一个具有模式 account_registration 的主体?您是否尝试将所有内容包装到 try/catch 中以检查错误?很抱歉这些问题,但是这台机器上没有 mongo 或 mongoose,所以不能自己测试看看它停在哪里。 -
它在 server.js console.log(request.method) 的第一行之一之前停止;在正常执行中,我在日志中得到了 OPTIONS 和 POST,但如果我删除最后一个响应,我只会得到 OPTIONS,就像其他调用没有及时发送一样......
标签: javascript jquery node.js http request