我在客户端使用javascript,在服务器端使用node.js,实际上我试图对输入字段进行远程验证,结果必须是布尔值,它应该基于服务器调用,我的代码如下,
jQuery.validator.addMethod("remoteValidation", function(value, element) {
var accountName = $("#accountName").val();
var accountType = $("#accountType").val();
var valid = this.Validation(accountType,accountName);
//returning undefined becoz return statement executing before server side execution
return valid;
}, "Entered email or username already exists");
this.validation = function(accountType,accountName,cb){
var valid =null;
ss.rpc('AccountsManagement.userNameChecker',accountType,accountName,function(res) {
// res will return boolean
valid = res;
});
//valid is null becoz this line is executing before valid = res line.
return valid;
};
我的服务器端代码:
services.imAccountsService.getAllImAccounts(accountType,accountName,
function(err,result){
if(err){
return;
}
if(result == null){
res(true);
}
else{
res(false);
}
});
我的问题是异步执行,在这种情况下如何使代码同步..