0

Im calling a .net svc service from jquery using Bootstrapvalidator.com. Im using the remote option to check if a username is already taken or not, see here: http://bootstrapvalidator.com/validators/remote/

The thing is dat my .svc services is returning the bool value like: {"d":true} while bootstrapvalidator is expecting {"valid":true}. I've read somewhere that microsoft uses the d for security reasons but i cant find this article anymore.

Question is, will i be able to return {"valid":true} or will my result always be {"d":true}. If the latter one is the case then i want to try to make the output like: {"d": [ "valid" : true ] } and hopefully bootstrapvalidator will do a .find for valid and maybe this will work. But im not sure how to create the output like that.

function:

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;
    }           
}
Sifu
  • 1,082
  • 8
  • 26
Blaataap
  • 21
  • 9
  • That has got to be possible to return your own defined JSON. I don't know much about web-services but perhaps this will help.http://stackoverflow.com/questions/288850/how-to-return-json-from-a-2-0-asmx-web-service – Rob Schmuecker Jul 23 '14 at 14:00
  • Heh, well reading that only confirms that the d cannot be dropped. It apperantly is added with 3.5 and cannot be undone. – Blaataap Jul 23 '14 at 15:04

2 Answers2

0

I think if you define a class with a valid property, and set that to true, that will serialize correctly. I think your current issue is you return a boolean directly, and the d is because it can't return true as a JSON object.

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

and do:

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 };
    }           
}
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • That was my first thought as well but I think the reason for the d is different, i just cant find the article or thread where i found the reason for the d. Hard to google d ;-) I've tried your solution and got the following result: {"d":{"__type":"AjaxService.X:#Webservices.Services","valid":true} } This didnt seem to work in combination with the bootstrapvalidtor sadly so i guess there is no solution to this issue. – Blaataap Jul 23 '14 at 14:22
  • 1
    Waiter there's a `d` in my soup! http://encosia.com/a-breaking-change-between-versions-of-aspnet-ajax/ – Rob Schmuecker Jul 23 '14 at 15:34
  • "d" is used to return primitives, and arrays (since returning an array directly can be compromised), but I didn't think it would be used for classes. – Brian Mains Jul 23 '14 at 18:05
0

OK since you're not having much joy with the other answers how about we hack BootStrapValidator!

OK well not "hack" more just extend http://bootstrapvalidator.com/examples/overriding-default-options/

Somewhere in the ether of github https://github.com/nghuuphuoc/bootstrapvalidator/blob/master/src/js/validator/remote.js around line 59 you'll find this:

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

This could be simply overridden/extended (by that you would have to extend/override the entire validate method) by changing it to this:

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

Small change I know. Waiter there's a d in my soup! http://encosia.com/a-breaking-change-between-versions-of-aspnet-ajax/

So I'm guessing that this will do the trick (to be called sometime after inclusion of the 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;
        }
    });
});
Rob Schmuecker
  • 8,934
  • 2
  • 18
  • 34