Problem with jQuery validate plugin (remote validation)
我在尝试使用jQuery Validation插件验证用户值时遇到问题。
验证似乎可以正确触发并完全按我的要求调用Web服务功能,但是即使服务器功能确实可以正常工作并返回
这是客户端上的验证代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | $('#myForm').validate({ errorContainer: container, errorLabelContainer: $("ol", container), wrapper: 'li', meta:"validate", rules: { Code: { required: true, maxlength: 15, remote: function () { return { type:"POST", url: GetBaseWSUrl() + 'MyService.asmx/IsValidCode', contentType:"application/json; charset=utf-8", dataType:"json", data: JSON.stringify({ umltCode: $('#Code').val() }) } } }, Description: { required: true, maxlength: 255 }, Notes: { maxlength: 255 } }, messages: { // ... omitted for brevity }, submitHandler: function (form) { saveObject(form); } }); |
使用提琴手,我可以看到对服务器的调用,并且服务器根据以下示例中的情况返回了json
1 | {"d":false} |
或
1 | {"d":true} |
尽管如此,插件仍将该字段标记为无效。有什么建议吗?
编辑:这是我的服务器功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [WebService(Namespace ="http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ScriptService] public class MyService : System.Web.Services.WebService { [WebMethod] public object IsValidCode(string umltCode) { [...] if (u != null) return false; return true; } } |
问题在于,而不是
1 | true |
您的Web服务返回
1 | {"d":true} |
ASMX被认为是旧版,因此我一开始就将其消除。但在那之前,您还可以使用
1 2 3 4 5 6 7 8 9 10 11 12 13 | remote: function() { return { type:"POST", url: GetBaseWSUrl() + 'MyService.asmx/IsValidCode', contentType:"application/json; charset=utf-8", dataType:"json", data: JSON.stringify({ umltCode: $('#Code').val() }), dataFilter: function (data) { var x = (JSON.parse(data)).d; return JSON.stringify(x); } }; } |