Reputation: 93
OK, for the life of me I can't uderstand why the follwing code is always returning false?? I have debugged with firefox to see what going on, but the function seems to always return false even when the condition username taken is 0(false), here is the jquery code:
function CheckAvailability() {
showLoader();
$.post("/Account/CheckUsernameAvailability",
{ userName: $(profile_username).val() },
function(data) {
var myObject = eval('(' + data + ')');
var newid = myObject;
if (newid == 0) {
profile_username_error.removeClass("field_not_valid");
profile_username_error.addClass("field_valid");
$("#validUserName_msg").html("<font color='green'>Available</font>")
return true;
}
else {
profile_username_error.addClass("field_not_valid");
profile_username_error.removeClass("field_valid");
$("#validUserName_msg").html("<font color='red'>Taken</font>")
return false;
}
});
}
I'm using the /Account/CheckUsernameAvailability to check if a given name is take or not, it not taken(0) should return true, false otherwise.
Upvotes: 1
Views: 328
Reputation: 4108
You don't seem to be allowing it to get the response back from the action call. What you need to do is to set off async call and wait for the callback to return.
$.ajax(
{
type: "POST",
url: "/Account/CheckUsernameAvailability",
data: "userName=" + $(profile_username).val(),
dataType: "json",
success: function(data) {
if (data.Success) {
// do success stuff
}
else {
// do failure stuff
}
}
});
The action which is called then can return some json code using a JsonResult.
public JsonResult CheckUsernameAvailability(string userName)
{
// do some checking with the Membership code
// this is just a simple class which can set the data and then
// be passed through to be sent back to the browser as Json
var result = new UiResult
{
Message = "Availble",
Success = true
};
return Json(result);
}
Hope this helps.
Upvotes: 1
Reputation: 532695
The post is done asynchronously so it is returning from your function before the post callback is even invoked. You should supply a callback to your function that can be invoked when the post completes and perform what ever action is needed in each case. Note that the callback can take a parameter which would ordinarily be the return value of your function (true/false).
The alternative would be to switch to using $.ajax and set the async
option to true, then capture the value to return in a local variable which you assign inside the ajax callback function.
I would stick with the callback idea since making the request synchronous can lock up your browser until it completes.
FWIW, I've found that the JSON parser works just fine -- you shouldn't need to use eval. Just return the data as an integer value and use it.
function(data) {
if (data && data.ID == 0) {
...
my_callback( true );
}
else {
...
my_callback( false );
}
...
Where your server-side code looks a bit like:
return Json( new { ID = user == null ? user.ID : 0 } );
Upvotes: 1
Reputation: 5239
In the snippet below, it looks like you are creating an object (newid) and then testing to see if that object equals the number zero. Isn't that always going to be false?
var myObject = eval('(' + data + ')');
var newid = myObject;
if (newid == 0) {
//stuff here
}
If your ajax script is literally just returning the number 1 as plain text, try casting data as an integer and testing against that.
var newid = parseInt(data);
if (newid == 0) {
//stuff here
}
Upvotes: 1