简体   繁体   中英

Parsley Remote and Server Side Validation (can't get content from json object)

I am trying to validate if an email already exists in our database. I always get a 200 status code from the Ajax Request, but I get the body of the request that looks like this. Result =1 if that email does not exists and is available (the form should return true) and Result = 0 if that email already exists in the database (the from should return false and not validate). However I keep geting data undefined in the console. Seems like I don't know how to pass the json object to the function.

I'll buy a beer to anyone that can help me figure this out. Thank you.

JSON RESPONSE

 id: "1c29da8e-ca94-4ea4-a69f-f0af15f54bf5", redirectPage: "/public/actionStatus.jsp", errorText: "",…} action: "/check.douserEmailAvailable" data: {} errorCode: "" errorText: "" id: "1c29da8e-ca94-4ea4-a69f-f0af15f54bf5" redirectPage: "/public/actionStatus.jsp" result: "1" text1: "" text2: "" text3: "" text4: "" text5: "" 

JQUERY and HTML

 $(document).ready(function(){ window.Parsley.addAsyncValidator('mycustom', function (data) { if (data.result == 1) { console.log("does not exist"); return true; } else { console.log("already exist"); return false; } }, '/check.do?action=userEmailAvailable&ajax=1'); $('#userReg').parsley(); }); 
  <input type="email" name="userEmail1" data-parsley-remote data-parsley-remote-options='{ "type": "POST", "data": { "token": "value" } }' data-parsley-remote-validator='mycustom' id="userEmail1" value="" required /> 

The first argument passed to your asynch validator is xhr , not data .

You can use $.parseJSON(xhr.responseText) , or xhr.done(function(json){...}) , etc... to do what you are trying to do.

Got it working, If anyone has issues in the future.

 $(document).ready(function(){ window.Parsley.addAsyncValidator('emailvalidation', function (data) { var myResponseText = data.responseText; var obj = jQuery.parseJSON(myResponseText); if (obj.result == 1) { return true; } else { return false; } }, '/check.do?action=userEmailAvailable&ajax=1'); $('#userReg').parsley().on('form:success', function() { var formData = $("#userReg").serializeArray(); var action = "<%=pdo.getRegistrationData().registrationType.toString()%>"; $.post("/userRegistration.do?action="+ action +"&ajax=1", formData, function(data) { window.location.href = data.redirectPage; }); }); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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