user384080
user384080

Reputation: 4682

issue with ajax call on success

have the following implementation

$.ajax({
type: "POST",
url: "/Member/SaveMember",
data: $('form').serialize(),
success: refreshGrid()

how come the refreshGrid() method is being called first prior to calling the ajax call of /Member/SaveMember ? I thought the "success" will only be called after successfully calling the server method.

any helps?

Upvotes: 0

Views: 65

Answers (2)

Roger Lindsjö
Roger Lindsjö

Reputation: 11543

You are setting up the ajax call with the result of calling your refreshGrid, not with refreshGrid as a reference. Remove the parenthesis to indicate that you do not want to call your method, but instead pass the method.

$.ajax({
type: "POST",
url: "/Member/SaveMember",
data: $('form').serialize(),
success: refreshGrid
});

If you want to set up the call with new parameters you wrap it in a new function

$.ajax({
type: "POST",
url: "/Member/SaveMember",
data: $('form').serialize(),
success: function() {
    refreshGrid(dataParameter);
}
});

Upvotes: 2

mgibsonbr
mgibsonbr

Reputation: 22007

Use refreshGrid only, without the parenthesis (when you use the parenthesis, you're calling the function, and attributing the result of the call to the success field).

Upvotes: 1

Related Questions