vzhen vzhen - 4 months ago 10
jQuery Question

Remove the previous ajax called result?

I am using jquery and I have 2 ajax call function. When I call the first ajax request then
the result will display at div id="result". Then I call the second ajax and I want it to display at div id="result" as well and replace the previous result which return from first ajax. From my tested, I still able to see the previous result under browser developer tools.

The second question is, if repeatly click will cause memory leak or not?

$.ajax({
url: "ajax/func1",
type: "post"
});

$.ajax({
url: "ajax/func2",
type: "post"
});

Answer

Then I call the second ajax and I want it to display at div id="result" as well and replace the previous result which return from first ajax.

Using the html() method should replace the content with the returned result every time. You can assign the result to your div in the done callback for example.

$.ajax({
  url: "ajax/func1",
  type: "post"
}).done(function(result) { 
  $("#result").html(result);
});

$.ajax({
  url: "ajax/func2",
  type: "post"
}).done(function(result) { 
  $("#result").html(result);
});

done is essentially the same as success but if I understood the comments in the documentation correctly is might be deprecated soon.

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks will be deprecated in jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

if repeatedly click will cause memory leak or not?

No, it should not cause a memory leak. All requests should execute after each other.