简体   繁体   English

使用JQuery $ .post进行验证

[英]Validation using JQuery $.post

I got a question concerning asynchronous calls. 我有一个关于异步调用的问题。 What I try to do is validate if a given "code" is valid. 我想做的是验证给定的“代码”是否有效。 Refreshing the page is not an option. 刷新页面不是一种选择。

The case in a nutshell: 简而言之:
I need to set a variable to true or false based on the reply from the server. 我需要根据服务器的回复将变量设置为true或false。
That call is done by using ajax. 该调用是使用ajax完成的。
The ajax ($.post) request processes the reply from the server and thus sets the variable to true or false. ajax($ .post)请求处理来自服务器的答复,因此将变量设置为true或false。
The value of the variable will then be used as the return value for that function, true or false. 然后,该变量的值将用作该函数的返回值,即true或false。
The problem is to only return AFTER the ajax has completed. 问题是仅在ajax完成之后返回。 The asynchronous behavior of ajax prevents this and hanging the browser is not done. ajax的异步行为可以防止这种情况,并且无法完成浏览器的挂起。

The validation is done by using a function (it will en up being used in an if). 验证是通过使用一个函数完成的(它将在if中使用)。

The if looks the following: if看起来如下:

if(codeGiven()) {
    // Code ok. Continue to process
} else { msgCodeInvalid(); }

The function codeGiven is the following 功能codeGiven如下

function codeGiven(toChk) {
    var codeOK = false; // default answer
    if(toChk) { // only perform call if toChk is not "" of undefined
        $.post(
            "validate.aspx", // server script
            { code: toChk }, // value
            function(data) { // function
                if(data == "true") {
                    codeOK = true; // when true => return will be true
                } else {
                    codeOK = false; // when false => return will be false
                }
            }
        );
    }
    return codeOK; // will be false by default but true when the code is OK
}

the aspx will use the posted "code" and match it against other codes in my database. aspx将使用发布的“代码”,并将其与数据库中的其他代码匹配。 If a match is found, the reply will be "true". 如果找到匹配项,则答复为“ true”。 If not, the reply will be "false". 如果不是,则答复为“假”。

Because an ajax call ($.post) is asynchronous, the result will always be false. 因为ajax调用($ .post)是异步的,所以结果将始终为false。

Any suggestions on how to hack my way out of this, besides my idea with a setInterval (a dirty method I know)? 除了我的想法与setInterval(我知道一个肮脏的方法)外,关于如何摆脱困境的任何建议吗? A return inside the post didn't work. 帖子内的退货无效。

Any ideas? 有任何想法吗?

Thanks in advance 提前致谢

Final solution 最终解决方案

I came to the realization that I could put my entire logic inside the $.post. 我意识到可以将整个逻辑放入$ .post中。

so basically, my logic is the following (warning, it's a deep tree structure): 所以基本上,我的逻辑如下(警告,它是一个深树结构):

if(myvar) { // if pid not empty
    $.post( // start validation
        "validator.aspx", // server side validation script
        { code : myvar }, // data to be passed to the server
        function(data, status) { // execute when post happend
            if(data == "true") { // if answer from server == "true"
                /*
                assign variables
                apply other logic
                process effects
                ...
                */
            } else { /* if answer from server == "false" => error: invalid code given */ }
        }
    ).error( /* error: validator did a boom boom. We're sorry :( */ );
} else { /* error: please enter a code */ }

Do your check in the callback or promise of your function, for example: 请检查函数的回调或承诺,例如:

function checkCodeGiven(toChk) {
    return $.post("validate.aspx", { code: toChk });
}

And then something like: 然后类似:

checkCodeGiven(someVar).then(function(data) {
    if(data == "true") {
        // do whatever you wanted to do for true
    } else {
        // do whatever you wanted to do for false
    }
});

you can : . 您可以 : 。 call the function you want inside the success function of the ajax 在ajax的成功函数中调用您想要的函数
OR 要么
. the delayer way :calling the part where you need the if(codeGiven) many times with delays about 100ms so you are sure that it's false or it becomes true at one call, replace this part 延迟器方法:多次调用需要if(codeGiven)的部分,延迟大约100ms,以便确保它是错误的或一次调用就变为真,请替换此部分

if(codeGiven()) {
// Code ok. Continue to process
} else { msgCodeInvalid(); }

with codeGiven();Delayer(0); 使用codeGiven();Delayer(0); and declair this 并拒绝这个

function Delayer(counter) {
        if (codeOK) {
            // Code ok. Continue to process
        } else if (counter <10) { //not sure if it may become true later
            setTimeout(Delayer, 100, counter++);} //call after 0.1 sec
          else {
            msgCodeInvalid();    // no hope to be true
        }
    }

plus codeOK should be declaired globally 加上codeOK应该在全球范围内发布
hope this works for you EDIT just clarified a bit 希望这对您有用编辑只是澄清了一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM