【问题标题】:Validation using JQuery $.post使用 JQuery $.post 进行验证
【发布时间】:2015-05-12 20:51:21
【问题描述】:

我有一个关于异步调用的问题。我尝试做的是验证给定的“代码”是否有效。刷新页面不是一种选择。

简而言之:
我需要根据服务器的回复将变量设置为 true 或 false。
该调用是通过使用 ajax 完成的。
ajax ($.post) 请求处理来自服务器的回复,从而将变量设置为 true 或 false。
然后变量的值将用作该函数的返回值,true 或 false。
问题是仅在 ajax 完成后才返回。 ajax 的异步行为阻止了这种情况,并且没有完成挂起浏览器。

验证是通过使用一个函数来完成的(它将在 if 中使用)。

if 如下所示:

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

函数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
}

aspx 将使用发布的“代码”并将其与我数据库中的其他代码进行匹配。如果找到匹配项,则回复将为“真”。如果不是,则回复为“假”。

因为 ajax 调用 ($.post) 是异步的,所以结果总是错误的。

除了我的 setInterval 想法(我知道的一种肮脏的方法)之外,还有什么关于如何破解我的方法的建议吗?帖子内的返回无效。

有什么想法吗?

提前致谢

最终解决方案

我意识到我可以将我的整个逻辑放在 $.post 中。

所以基本上,我的逻辑如下(警告,这是一个深度树结构):

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 */ }

【问题讨论】:

  • 我知道由于异步行为,我的第一种思维方式行不通。我想过用 setInterval() 来解决这个问题(在 timeoutCount != expired && !postExec 时继续循环。然后在 postExec == true 时执行代码),但后来我意识到我可以把我所有的代码都塞进没有冲突的回调。隧道视野是一件坏事,唯一摆脱它的方法就是把项目放在一边,然后带着新的想法回来。
  • 或者,更好的是,使用 Promise。

标签: javascript jquery ajax validation


【解决方案1】:

检查函数的回调或承诺,例如:

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

然后是这样的:

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
    }
});

【讨论】:

  • 没试过这个,但是第二天早上我找到了解决问题的方法。睡眠能给人的心灵带来美妙的东西。我决定把我所有的逻辑都放在 $.post 的回调中。它没有干扰其余部分,因此有效。
【解决方案2】:

你可以: .在ajax的成功函数中调用你想要的函数

.延迟方法:多次调用需要 if(codeGiven) 的部分,延迟约 100 毫秒,因此您确定它是错误的,或者一次调用就变为真的,替换这部分

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

codeGiven();Delayer(0); 并声明这个

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
        }
    }

加上codeOK应该全局声明
希望这对你有用 编辑只是澄清了一点

【讨论】:

  • setTimeout 会挂起浏览器,我不想这样做。无论如何,感谢您的建议,但我设法在没有计时器的情况下解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2013-08-09
  • 1970-01-01
  • 1970-01-01
  • 2011-02-21
相关资源
最近更新 更多