Vishnu Prem
Vishnu Prem

Reputation: 33

how to get a page on the middle of an ajax request

I have a page, when loaded, an ajax call is made using jquery. It will take some 5-6 seconds to complete the request. And I want other events to happen simultaneously. My problem is : if I navigate to any other page (clicking a menu item for eg:) before completing the ajax request, it will get redirected to home page. Here is the ajax request:

$.ajax({
  async: true,
  mode: "abort",
  type: 'POST',
  url: '/foo/bar',
  data: params,
  dataType: 'json',
  success: function (data) {
  /* do some */
  }
});

I think that there needs to be some additional parameters for Ajax request. But I am not sure.

Upvotes: 0

Views: 121

Answers (2)

Vishnu Prem
Vishnu Prem

Reputation: 33

Got the answer today. Its the parameter global of jQuery Ajax,, we have to set, to false. Ref : http://api.jquery.com/jQuery.ajax/

Upvotes: 1

sushil
sushil

Reputation: 2671

I don't think its the ajax request thats causing problem. Instead your menu items could have been the cause of the problem. I think your menu items are created by using some divs on which you have jQuery event 'click' is binded. eg:

<a href="#some_div_id_for_secondary_menu" id="menu_item" href>Primary Menu</a>

On this div, you must be having some jQuery function:

$('#menu_item').click(function(){
   ...
   ...
});

If this is the case, placing above jQuery snippet in the below mentioned code snippet will solve your problem.

$(document).ready(function(){

)); 

Upvotes: 0

Related Questions