开发者

jquery.validate addMethod error displays even true is retained

I am using additional method for checking capcha validation using jquery.validate

 $.validator.addMethod("ajax_validation", function(value) {
  var ajax_data='ajax_mode=capcha_validation&code='+value;
   $.ajax({
      t开发者_运维知识库ype: "POST",
      url: "/app/ajax_validation/",
      data: ajax_data,
      dataType: "text",
      success: function(msg){
    if(msg=='true')
    {
     alert('true');
     return true;
    }
    else
    {
     alert('false');
     return false;
    }

      }
   });
 }, 'Please enter code correctly!');

Even when i retain true value error is always displayed on screen how to handle this.


If you want to catch the result and go any further, you need to call jquery ajax in sync format. In your code, whatever the result is, the default behavior (which should be an error message, I think) happens.

Don't forget that jQuery Ajax call are Async by default ("A" from "Async" is Asynchronous) and if you want to do ajax call in sync manner, you need to do turn it to 'false' like this:
(I put your method in a separate function for clarity)

    function ajax_validation(ajax_data) {
          var ajax_data='ajax_mode=capcha_validation&code='+value;          
          var result;
          $.ajax({
              type: "POST",
              async: false,
              url: "/app/ajax_validation/",
              data: ajax_data,
              dataType: "text",
              success: function(msg){
                     result= msg.d;
               }  
           });
           return result; 
    }     
        //...

However, note that ajax Sync calls may freeze the browser and generally is not a good practice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜