Return bool value from ajax

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.


    var alreadyExists = function () {
        var role = $("#UserRole").val();
        var notexists = false;

            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;


  1. I just declared a variable that contains false initially i.e.. var notexists = false;
  2. Added async: false, just after type. DO NOT MISS THIS STEP!
  3. Stored the value in the variable based on the conditions in success e.g. notexists= true or notexists=false
  4. Finally, returned the variable contain bool value just before the function alreadyExists ends e.g. return notexists;

Now, You are good to go! 🙂

2 thoughts on “Return bool value from ajax

Add yours

  1. 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.



Leave a Reply to CodeBlogs Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at

Up ↑

Create your website with
Get started
%d bloggers like this: