0

I am using Ajax function to add product in cart in MVC 3

In Ajax i have a function for adding product, inside that function i want to call a another function but its not working..

My Ajax function is

var AjaxCart = {

  addproductvarianttocart: function (urladd, formselector) {
    if (this.loadWaiting != false) {
        return;
    }
    this.setLoadWaiting(true);

    $.ajax({
        cache: false,
        url: urladd,
        data: $(formselector).serialize(),
        type: 'post',
        success: this.successprocess,
        complete: this.resetLoadWaiting,
        error: this.ajaxFailure
    });

  refreshPage();

},


refreshPage: function () {

    $.post('/ShoppingCart/OrderSummaryChild', function (data) {
        alert("Inside2");
        // Update the ItemList html element
        $('#CartUpdatePanel').html(data);
        alert("Out");
    });

}
};

The link is from where i am calling addproductvarianttocart function

<a onclick="AjaxCart.addproductvarianttocart( '/addproductvarianttocart/25/1/');return false;">
Avinash Singh
  • 2,527
  • 10
  • 38
  • 68

1 Answers1

0

The ajax call is asynchronous. You should put the function refreshPage inside the success or complete function. This way the refreshPage function will be called right after the ajax call is finished and the page is ready to be refreshed with the new data.

Extracted from jQuery api:

Description: Perform an asynchronous HTTP (Ajax) request.

Vic
  • 622
  • 6
  • 15
  • I did like this also but Refresh function is not calling successprocess: function (response) { if (response.message) { if (response.success == true) { refreshPage(); } } – Avinash Singh Jun 13 '13 at 11:58
  • any message in the console? – Vic Jun 13 '13 at 12:09
  • i have given loading image in addproduct function, that only showing continuously – Avinash Singh Jun 13 '13 at 12:12
  • which version of jquery are you using? – Vic Jun 13 '13 at 13:21
  • Ok. Let's continue, i cannot see where do yo pass the formselector parameter, so its empty. And where are the other functions defined. When i asked you what does the console tell you i meant in the Firebug conosole for Firefox or similar in other browsers. There you should find some info. I hope :) Any new info you get, please share. – Vic Jun 13 '13 at 17:40
  • Actually its taking time to post data, for that i have taken setTimeout(function () { return true; }, 10000); for waiting then updating CartUpdatePanel Div, Now its working fine : ) – Avinash Singh Jun 14 '13 at 05:28