-1

I have the following code in jQuery. I have e.stopImmediatePropagation() and return false so that the code does not continue to where it does the POST. How can I prevent the code from continuing?

$("#saveBtn").on("click", function (e) {

       var obj = { name: $("#sName").val() };
       $.ajax({
                url: '@Url.Action("CheckExist", "PMP")',
                type: "GET",
                async: true,
                data: obj,
                success: function (data) {
                    if (data != null) {                          

                        if (data == 'True')
                        {
                            alert('HERE');                               
                            e.stopImmediatePropagation();
                            return false;
                         }

                    }
                }
        });

       // I WANT THE CODE TO NOT DO THE POST IF data == 'True' but it seems to continue regardless of return false
       $.ajax({
            type: "POST",
            url: data_appRootPath + "@Url.PMPActionUrl((object)@ViewBag.Org_ID,                 
            "GetType", null, null, null)",
            contentType: false,
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – freedomn-m Sep 22 '20 at 16:32
  • Your `CheckExist` call is *aschronous* - the PMPActionUrl runs *before* your `success:` callback runs – freedomn-m Sep 22 '20 at 16:32

1 Answers1

1

The thing that you do are two async operations. This means that the both ajax requests are being fired at the same time (in parallel). When the response from the ajax request comes back, it's already late to cancel the second ajax request.

If you want this to happen, you need to fire the second request in the callback of the first request.

So, something like that:

$("#saveBtn").on("click", function (e) {

       var obj = { name: $("#sName").val() };
       $.ajax({
                url: '@Url.Action("CheckExist", "PMP")',
                type: "GET",
                async: true,
                data: obj,
                success: function (data) {
                    if (data != null) {                          

                        if (data == 'True')
                        {
                           // I WANT THE CODE TO NOT DO THE POST IF data == 'True' but it seems to continue regardless of return false
                           $.ajax({
                                type: "POST",
                                url: data_appRootPath + "@Url.PMPActionUrl((object)@ViewBag.Org_ID,                 
                                "GetType", null, null, null)",
                                contentType: false,
                         }

                    }
                }
        });

Stavros Zavrakas
  • 3,045
  • 1
  • 17
  • 30