-1

I have the following ajax request being called:

$.ajax({
    type: "POST",
    url: "/admin/RoutingIndicators/Add",
    data: { newSecondaryRI: newRi },
    success: function () {
        alert('hit');
        document.location.reload(true);

    },
    error: function (xhr, status, error) {
        var err = eval("(" + xhr.responseText + ")");
        alert(err.Message);
    }
});

The add method in the controller executes fine and returns below:

return Json(JsonResponses.Success());

However, none of the functions in the ajax request (success, error, complete) fire after the method returns success the first time around. After the second go, the first value that I added shows up, but not the second. If I reload the page, the other one shows up, so I know the Add method is working correctly. What could be causing the skip over the methods in the ajax request for the first iteration?

ekad
  • 13,718
  • 26
  • 42
  • 44
Sean Smyth
  • 1,209
  • 3
  • 23
  • 42
  • Where are you reading the value in the success? Where are you updating tge page after you make the Ajax call? All I see is you reloading the page – epascarello Feb 18 '15 at 17:40
  • Reading the value in the success? What do you mean? I'm updating the page on that reload. The true is reloading the data from the server, then goes to an Index ActionMethod that resets the data displayed on the page. But that's working fine. @epascarello – Sean Smyth Feb 18 '15 at 17:53
  • I have no clue what your problem is. So you are saying the Ajax call fails? Are you sure it has anything to do with the Ajax call? Are there any errors in the console? That error code looks wrong to me. There is not going to be a JSON response if the server returns an error so that will be a bug. – epascarello Feb 18 '15 at 18:01
  • I outlined the problem above. It's going to the controller, executing the method correctly, no errors are thrown, but it's still not hitting my success function. That's the issue. And no, there are no errors in the console. – Sean Smyth Feb 18 '15 at 18:25
  • Well what do you see in the console with the Ajax request? Inspect the call. – epascarello Feb 18 '15 at 18:29
  • Do things "work" -- other than failing to display your updates -- if you remove the "reload()" step? Sorry if this seems basic, but are you aware that when you "reload" the document, the entire browser reloads and your page starts over, so in a real sense there's no "second go"? It's just running once, starting from a different state. – Kevin Crumley Feb 18 '15 at 18:30
  • I'm not sure where you're looking for the response to be, but jQuery's `success` property is a Javascript callback. If you give it an argument (`success: function(response) { alert(response); }`) you can get the response. – Scott Feb 18 '15 at 18:34
  • @kcrumley The reload is not hit until the 2nd iteration, so I'm not worried about it. It seems to work correctly when it's hit, it's all a matter of getting the first iteration to hit the success function – Sean Smyth Feb 18 '15 at 23:19

1 Answers1

0

So I found a workaround. If I set async to false in my ajax request, then the success function is hit and everything works as it should.

Sean Smyth
  • 1,209
  • 3
  • 23
  • 42