Remote jquery validation plugin request - error / failure handling

I am using a jquery validation plugin in the form of editing a user profile. I use the 'remote' function to request my website and verify the uniqueness of the email, which works well. My question is how to deal with bugs or crashes that do not seem to be well documented. The behavior I want is that if the JSON request fails or the time runs out, forget this part of the check, allow the user to send and allow the check on the server side.

I see that there is an error attribute, and it looks like you can add a function to run in this scenario, but what would I put there to disable the validation rule in case of timeout / error / failure?

remote: { url: location.pathname + "/EmailUnique", timeout: 20000, data: { userID: function() { return $("#Form_EditProfileForm_userID").val(); } } } 
+4
source share
1 answer

Yes, add the error function:

 remote: { //your normal $.ajax stuff here , error: function(jqXHR, textStatus, errorThrown) { //deal with the error } } 

All these parameters are passed to a regular jQuery ajax call, so by specifying the error function, you can handle whatever happens.

Assuming you saved the validation object in the variable v , your error function might be so simple:

 $('#inputWithRemote').rules('remove','remote');. v.pendingRequest--; 

Honestly, the v.pendingRequest business is about what I would call an error in the Validation plugin - it increases this pendingRequest counter when it launches an ajax request, but does not have its own code to solve errors in the response.

See here: http://jsfiddle.net/ryleyb/H96TD/ (note that I set the timeout to 2 seconds and used some jsfiddle special ajax code to force the response to take 3 seconds).

+3
source

Source: https://habr.com/ru/post/1394042/


All Articles