簡體   English   中英

Parsley遠程和服務器端驗證(無法從json對象獲取內容)

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

我正在嘗試驗證我們的數據庫中是否已存在電子郵件。 我總是從Ajax請求中獲得200狀態代碼,但是我得到的請求主體看起來像這樣。 如果該電子郵件不存在並且可用(結果應該返回true),則Result = 1;如果該電子郵件已經存在於數據庫中(則from應該返回false並且不驗證),則Result = 0。 但是,我一直在控制台中獲取未定義的數據。 好像我不知道如何將json對象傳遞給函數。

我會買啤酒給任何可以幫助我解決這個問題的人。 謝謝。

JSON響應

 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和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 /> 

傳遞給異步驗證器的第一個參數是xhr ,而不是data

您可以使用$.parseJSON(xhr.responseText)xhr.done(function(json){...})等來完成您想做的事情。

如果任何人將來遇到問題,都可以使用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM