如何返回response.valid而不是response.d ASP.NET ajax

How to return response.valid instead of response.d ASP.NET ajax

本文关键字:response ASP ajax NET valid 何返回 返回      更新时间:2023-09-26

在我的asp.net网站上,我需要一个简单的WebMethod,它可以返回jquery ajax:

{ "valid": true } or { "valid": false }

http://bootstrapvalidator.com/validators/remote/

为此,我使用代码:

public class IsValid
    {
        public bool valid { get; set; }
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static IsValid ValidUser(string userName)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        var jsonData = new IsValid
        {
            valid = true
        };
        if (userName.IsEmpty())
        {
            // it's for other validator.
            return jsonData; 
        }
        User user = DbContext.Entity.Users.FirstOrDefault(c => c.UserName == userName.Trim());
        if (user != null)
        {
            jsonData = new IsValid
            {
                valid = false
            };
        }
        return jsonData; 
    }

但它在response.dvalid中返回值,而不是在js函数中返回response.valid

xhr.then(function(response) {
                dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true', response.message ? response.message : null);
            });

我应该如何更改代码以返回response.valid?谢谢

不幸的是,您无法调整Framework 3.5和plus Services以不返回".d"。但如果您对这样的编码进行了一些更改,则可以使用response.valid.

 [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string ValidUser(string userName)
    {
           JavaScriptSerializer js = new JavaScriptSerializer(null);
            jsonData = new IsValid
            {
                valid = false
            };
        return js.Serialize(jsonData);
    }

xhr.then(function(response) {
                response = jQuery.parseJSON(response);
                dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true', response.message ? response.message : null);
            });