2

クライアント側で 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);
    }

   });

私の問題は非同期実行です。この状況でコードを同期させる方法..

4

2 に答える 2

1

jquery 検証メソッドに関数を追加しました。

   if(param.type === "rpc"){
            var args = param.data.remoteArgs();
            ss.rpc(param.url, args, function(response) {
                validator.settings.messages[element.name].remote = previous.originalMessage;
                var valid = response === true;
                if ( valid ) {
                    var submitted = validator.formSubmitted;
                    validator.prepareElement(element);
                    validator.formSubmitted = submitted;
                    validator.successList.push(element);
                    validator.showErrors();
                } else {
                    var errors = {};
                    var message = response || validator.defaultMessage( element, "remote" );
                    errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message;
                    validator.showErrors(errors);
                }
                previous.valid = valid;
                validator.stopRequest(element, valid);
            });
        }

それは正常に動作します....すべてに感謝します...

于 2012-11-05T12:50:45.593 に答える
0

resの値が利用可能になるまで待つ必要があります。以下のコードが機能するかどうかを確認してください。

    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

            while(res <> null)
            {
               window.setInterval(function(){waitFunc()}, 1000);
            }
            valid = res;
        });
                //valid is null becoz this line is executing before valid = res line.
        return valid;
    };

function waitFunc()
{
//do nothing
}
于 2012-11-05T07:08:19.977 に答える