.svc Web服务返回一个json对象

时间:2014-07-23 13:54:03

标签: jquery web-services

我使用Bootstrapvalidator.com从jquery调用.net svc服务。我使用远程选项来检查是否已经使用了用户名,请参阅此处:http://bootstrapvalidator.com/validators/remote/

事情是dat我的.svc服务返回bool值,如:{“d”:true},而bootstrapvalidator期待{“valid”:true}。我已经读过某个地方,微软出于安全考虑使用d,但我不能再找到这篇文章了。

问题是,我能否返回{“valid”:true}或者我的结果总是{“d”:true}。如果后者是这种情况,那么我想尝试输出如下:{“d”:[“valid”:true]}并希望bootstrapvalidator将执行.find有效,也许这将工作。但我不知道如何创建这样的输出。

功能:

public bool CheckUsername(string userName) {
    try {
        using (var dbC = new DataContext(ConfigurationManager.ConnectionStrings[_environment].ToString())) {
            bool valid = false;
            var check = dbC.UserSelectByUsername(userName).ToList();
            if (check.Count() > 0) {
                return valid;
                }
            else {
                return valid = true;
                }
            }
        }
    catch (Exception exc) {
        Log.Error("Error in .", exc);
        return false;
    }           
}

2 个答案:

答案 0 :(得分:0)

我认为如果您定义一个具有有效属性的类,并将其设置为true,那么将正确序列化。我认为你当前的问题是你直接返回一个布尔值,而d是因为它不能作为JSON对象返回true。

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

并且做:

public bool CheckUsername(string userName) {
    try {
        using (var dbC = new DataContext(ConfigurationManager.ConnectionStrings[_environment].ToString())) {
            bool valid = false;
            var check = dbC.UserSelectByUsername(userName).ToList();
            if (check.Count() > 0) {
                return new X { valid = valid };
                }
            else {
                return new X { valid = true };
                }
            }
        }
    catch (Exception exc) {
        Log.Error("Error in .", exc);
        return new X { valid = false };
    }           
}

答案 1 :(得分:0)

好的,因为你对其他答案没有太大的兴趣我们如何破解BootStrapValidator!

好吧不要" hack"更多只是extend http://bootstrapvalidator.com/examples/overriding-default-options/

在第59行的github https://github.com/nghuuphuoc/bootstrapvalidator/blob/master/src/js/validator/remote.js的以太某处,你会发现这个:

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

这可以简单地覆盖/扩展(通过你必须扩展/覆盖整个validate方法),将其改为:

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

我知道的小变化。 服务员在我的汤里有d http://encosia.com/a-breaking-change-between-versions-of-aspnet-ajax/

所以我猜测这会解决问题(在包含BootstrapValidator javascript后的某个时间调用):

$(document).ready(function () {
    $.fn.bootstrapValidator.validators.remote = $.extend({}, $.fn.bootstrapValidator.validators.remote, {
        validate: function (validator, $field, options) {
            var value = $field.val();
            if (value === '') {
                return true;
            }

            var name = $field.attr('data-bv-field'),
                data = options.data || {},
                url = options.url,
                type = options.type || 'POST';

            // Support dynamic data
            if ('function' === typeof data) {
                data = data.call(this, validator);
            }

            // Support dynamic url
            if ('function' === typeof url) {
                url = url.call(this, validator);
            }

            data[options.name || name] = value;

            var dfd = new $.Deferred();
            var xhr = $.ajax({
                type: type,
                url: url,
                dataType: 'json',
                data: data
            });
            xhr.then(function (response) {
                dfd.resolve($field, 'remote', response.d.valid === true || response.d.valid === 'true', response.d.message ? response.d.message : null);
            });

            dfd.fail(function () {
                xhr.abort();
            });

            return dfd;
        }
    });
});
相关问题