jQuery验证插件的问题(远程验证)

Lor*_*nzo 4 asp.net jquery web-services jquery-validate

我尝试使用jQuery Validation插件验证用户值时遇到问题.

验证似乎正确启动并完全按照我的意愿调用Web服务函数,但即使服务器功能正常工作并返回true/ false结果,该字段始终无效.

这是客户端的验证代码

$('#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);
    }
});
Run Code Online (Sandbox Code Playgroud)

使用fiddler我能够看到对服务器进行了调用,并且服务器正在返回json true/ false值,具体取决于案例,如下例所示:

{"d":false}
Run Code Online (Sandbox Code Playgroud)

要么

{"d":true}
Run Code Online (Sandbox Code Playgroud)

尽管如此,该插件仍将该字段标记为无效.有什么建议吗?

编辑:这是我的服务器功能

[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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 7

问题是,而不是

true
Run Code Online (Sandbox Code Playgroud)

您的Web服务返回

{"d":true}
Run Code Online (Sandbox Code Playgroud)

d属性应删除.

ASMX被认为是遗产,所以我会在第一时间摆脱它.但在此之前,您还可以使用该dataFilter属性,如下所示:

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); 
        }  
    };   
}
Run Code Online (Sandbox Code Playgroud)