Javascript 如何处理异步的ajax响应
我想从nodejs中的数据库中检索一些数据。 检索这些数据是一个异步请求,我们也可以使用ajax请求 Client.jsJavascript 如何处理异步的ajax响应,javascript,jquery,ajax,node.js,asynchronous,Javascript,Jquery,Ajax,Node.js,Asynchronous,我想从nodejs中的数据库中检索一些数据。 检索这些数据是一个异步请求,我们也可以使用ajax请求 Client.js $.ajax('localhost/Server').done(function(){ }); Server.js function cb(){ // do stuff } // ajax request is going here function Server(req, res) { GetTheModelFromTheDbInAsyncWay(f
$.ajax('localhost/Server').done(function(){
});
Server.js
function cb(){
// do stuff
}
// ajax request is going here
function Server(req, res) {
GetTheModelFromTheDbInAsyncWay(function(cb){
cb();
});
}
function GetTheModelFromTheDbInAsyncWay(cb) {
//doing stuff to the db e.g getting the result of a query
// ...
cb(result);
}
在异步ajax请求中检索aync服务器请求需要使用什么技术?
我认为这将是类似于承诺的。
但是如何将其传递回ajax请求,因为db请求本身是异步的
希望我能够清楚地说明这一点,您正在调用从
GetTheModelFromtheBinasyncway
收到的参数,就好像它是一个函数一样。大概不是。您应该使用它(例如,通过res.send
)发送它或从中派生的信息):
您正在调用从
GetTheModelFromtheBinasyncway
接收的参数,就好像它是一个函数一样。大概不是。您应该使用它(例如,通过res.send
)发送它或从中派生的信息):
哦,我感觉很糟。^^@xhallix:我们都去过那里。:-)哦,我感觉很糟。^^@xhallix:我们都去过那里。:-)
// ajax request is going here
function Server(req, res) {
GetTheModelFromTheDbInAsyncWay(function(data){ // Not `cb`, `data`
// Use `data` here to produce the response you send via `res`
});
}