Returning bool value from ajax is not similar to any simple function that returns the bool value. BUT DON’T WORRY! it isn’t even too difficult. it’s just slightly different.
Solution
var alreadyExists = function () {
var role = $("#UserRole").val();
var notexists = false;
$.ajax
({
url: "@(Url.Action("checkExists", "UserManagement"))",
data: { role: role },
contentType: "application/json; charset=utf-8",
type: 'GET',
async: false,
dataType: "json",
success: function (result) {
if (result.length > 0)
{
alert("This User Role already exists");
notexists = false;
}
else {
notexists = true;
}
}
});
return notexists;
}
Explanation
- I just declared a variable that contains false initially i.e.. var notexists = false;
- Added async: false, just after type. DO NOT MISS THIS STEP!
- Stored the value in the variable based on the conditions in success e.g. notexists= true or notexists=false
- Finally, returned the variable contain bool value just before the function alreadyExists ends e.g. return notexists;
Now, You are good to go! 🙂
Hey thеre! I understand tһis іѕ kid ߋf off-topic but
I һad to asҝ. Dⲟes running a well-established blog ⅼike yoᥙrs takе a ⅼot off work?
I am сompletely new tօ running ɑ blog howevеr I do write in my journal everyday.
Ι’ⅾ liкe to start a blog so I will Ƅe aƅle to share my experience and th᧐ughts online.
Pⅼease let me know if you have any kіnd of recommendations or tips fоr new aspiring blog owners.
Thankyou!
LikeLike
Hi
I just started last month. Just write what you want and do some SEO work or hire an SEO expert. You’ll be good to go then 🙂
Thanks
LikeLike