0

I'm beginner at javascript so learning constantly very many new things so dont be angry if im asking a really stupid question because still learing the ropes. Trying to implement callback instead async: false. Adding this method to jquery Validator plugin. With async false works fine, but i don't wanna use it. Any help appreciated :)

$.validator.addMethod( "CheckIfUsed",

function(value, element) {
var isSuccess
$.ajax({url :"/x/validate",
    data :  JSON.stringify({model:$(element).data('model'),
    field:element.name,
    equals:value}),
    contentType : 'application/json',
    type : 'POST',
    async: true,
    success: function(res){
                { isSuccess = res === "true" ? true : false }
        }
    })

return isSuccess
 },"That data allready used");

Tryd this: Javascript callback functions with ajax - did not work

Or am i doing something copletly wrong

10
  • Have you looked at the "remote" validation included in the plugin? jqueryvalidation.org/remote-method Commented Aug 15, 2016 at 20:29
  • not completly, dont remeber what went wrong with that approach. could not get it work at all Commented Aug 15, 2016 at 20:43
  • Is there a reason that "remote" will not work in your case, the example in the documentation is almost exactly what you describe here. Commented Aug 15, 2016 at 20:43
  • Im note quite understaning how it helps with the async. Problem at the moment is with async true it wount waite for the data. If im doing it like that problem remains or am i wrong? Commented Aug 15, 2016 at 20:55
  • Just know that you will never get anywhere with your method right now with asnyc: true. It will never wait for a result before returning null Commented Aug 15, 2016 at 21:28

1 Answer 1

0

From the documentation the remote method does almost exactly what you describe. https://jqueryvalidation.org/remote-method/ here is an example that I use in many parts of my current code-base.

$("#SIN").rules("add", {
        remote: {
            url: '/x/ValidateUsed',
            async: true,
            data: {
                sin: function () { return $("#SIN").val(); },
                clientId: $("#ClientID").val()
            }
        }
    });

Just remember that "validate" or in my case "ValidateUsed" needs to return true if not used or null, undefined, or a custom message like That data already used if it is in use.

Sign up to request clarification or add additional context in comments.

4 Comments

i dont wana use async: false because -> synchronous xmlhttprequest on the main thread is deprecated because of its detrimental effects to the end users experience. for more help, check...
This takes all options that a full jQuery.ajax call does so as I understand it you can add whatever callbacks you desire to it.
it works fine with async, but thanks to that message (sync..) i wana remove it. im adding call back somehow wrongly so it dosent get the data. Can you modify my code so it woult be correct? im having hard time what im doing wrong or what am i missing
Can you create a jsfiddle with example HTML and paste your javascript in there. I can modify your fiddle for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.