views:

213

answers:

2

Hello,

I am using jQuery's validation plugin to validate a username. Somehow my json request data is invalid and I dont know why. Here is the code:

remote: {
                    url: "blablabla",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: '{"username":"' + function () { return $("#username").val() } + '"}'
                }

When I check out the request, the json data looks like this:

{"username":"function () {
    return $("#username").val();
}"}

Do you have any advice?

A: 

Looks like your function is not evaluating. Can you perhaps run it before the call and put the actual return value in the data?

DroidIn.net
If I rewrite it like this: "data: '{"username":"' + getUsername() + '"}'", then it works fine, however I would really love to use an anonymous function in this case.
s7orm
run it through evil eval in onSuccess
DroidIn.net
Hmmm - I wonder if this will work`data: '{"username":"' + function () { return $("#username").val() } + '"}()'`
DroidIn.net
Isn't onSuccess too late? I mean I want to execute the function when calling the webservice, and as far as I know, onSuccess is called when receiving a response?Regarding your second idea - it didn't work :(
s7orm
So you want to pass user name value to the server, validate and then ether accept or fail? In which case if you have an error (say duplicate name) you should just fail ajax call and then do your processing in onError call. Your callback will not be evaluated on the server - it's only can happen on the client
DroidIn.net
Yep, I want to validate a username on the server, however the whole communication process fails when constructing the request.
s7orm
So construct it with value of your filed rather than function that grabs the value of your filed. You could be over-engineering a rather simple thing
DroidIn.net
Well, if I use - data: '{"username":"' + $("#username").val() + '"}' than I always receive an empty string on the other end... I am not exactly sure why, but I think this expression is evaluated only once - onready, but if I use a function instead - the expression is reevaluated every time.
s7orm
Its evaluated when the ajax call is made, not once when the document loads unless you are caching the ajax request object on document ready, which would be silly since it would invalidate with input.
Alex Sexton
You mention that you are using jquery validate - that's client-side plugin. If you want to do a server side validation what you need to do is to have event handler attached to submit button (optionally to onBlur of your text field) in which 1. you capture value of your input, 2. issue AJAX call to the server which validates it.I don't know if jQuery Validate has this support out-of-the box but I suppose you can extend it to provide what you need
DroidIn.net
jQuery validate has native support for inline serverside validation on events like blur/change.
Alex Sexton
Not disputing that - it's been a while
DroidIn.net
Yes, I am using jquery validate, which is a client side plugin, however the plugin allow a "remote" validation by using ajax calls.@ Alex: The whole validation code is inside jquery's ready function, but I dont know if this is considered caching.
s7orm
A: 

To write a self executing function wrap it in parenthesis first:

(function () { return $("#username").val() })()
Alex Sexton
I tried that, however I am getting an empty string in the webservice (and not the actual value of the textbox)
s7orm
What do you mean by "in the webservices"? That callback only makes sense if it's executed in your browser which has box with "username" id. Can you elaborate?
DroidIn.net
Well the function (wrapped like this) returns an empty string "", rather than the actual value.
s7orm
I'm confused why you are encoding everything as string, to be honest. I believe you can just set `data: {var1: 'asdf', var2: 'sdfg'}` and jquery should handle the rest. Also, a self executing function doesn't gain you anything in this case, any reason you are trying to use it?
Alex Sexton
Everything is encoded as a string, because of the webservice side (wcf). And I am using a self executing function, because you gave me the idea...
s7orm
I was just executing the function you had in there. Take it out entirely and just use `$("#username").val()` not a function call- And instead of parsing the string together (which you still probably dont really need as a string) use the json2.js library to convert it to json from the object literal with JSON.stringify(). -- http://www.json.org/json2.js
Alex Sexton