-1

The below AJAX calls work (notice the first one calls the second one on success), however on success of the second call it reloads the browser, and the changes are not always displayed immediately without the user having to manually reload the page a second time. Any way to fix this behavior?

 self.assign = function () {

        var data = {
            selectedViewsForClient: self.selectedViewsForClient().toString(),
            volPoolIds: populateSelectedVolPoolIds().toString()
        };

        var jsonData = ko.toJSON(data);

        $.ajax({
            url: "../../act/ClientView/assign",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            cache: false,
            data: jsonData,
            success: self.removeViews,
            error: handleEditViewsErrors
        });                        
    };

    self.remove = function () {

        var data = {
            selectedViewsForClient: self.selectedViewsAssignedToCandidateForClientUser().toString(),
            volPoolIds: populateSelectedVolPoolIds().toString()
        };

        var jsonData = ko.toJSON(data);

        $.ajax({
            url: "../../act/ClientView/remove",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            cache: false,
            data: jsonData,
            success: location.reload(),
            error: handleEditViewsErrors
        });
    };        
Dean Friedland
  • 773
  • 2
  • 12
  • 32
  • `success: location.reload()` syntax/logic error. You're initiating the reload before the second ajax request is even sent. – Kevin B May 10 '17 at 15:59
  • possible duplicate: http://stackoverflow.com/questions/32467369/why-doesnt-settimeout-wait-to-call-a-callback – Kevin B May 10 '17 at 16:01

2 Answers2

0

location.reload() reloads the page from cache, to force the page to refresh from the server, use location.reload(true), where the true forces a server refresh

Barmar
  • 741,623
  • 53
  • 500
  • 612
rem
  • 85
  • 1
  • 11
-1

I added async: false to my second call and it worked like a charm!

Dean Friedland
  • 773
  • 2
  • 12
  • 32
  • Always [be wary](http://stackoverflow.com/questions/6517403/what-are-the-drawbacks-of-using-synchronous-ajax-call) when using synchronous AJAX calls... – Sandman May 10 '17 at 16:06
  • If you can show me a better way to do it I am all ears. I did not see a suggestion. – Dean Friedland May 10 '17 at 16:32