views:

52

answers:

1

I'm using jQuery getScript in Rails to load an AJAX search on a dashboard page. I just noticed, though, that in addition to properly making the call it's ALSO reloading the entire page (in the background).

I have no idea why this happening.

I checked all my before_filters, all my authentication logic, I tried using different jQuery ajax functions (get, getJSON, etc.), but nothing. it's still reloading the page. also, the two routes are even on different controllers!

Does anybody know what might be going on?

EDIT:

RESOLVED.

I was using an $.ajax({}) function in addition to a $.get() function in order to set a before function. Something in the $.ajax must have been triggering the call, so I simply merged the new functions into one and it resolved my problem.

BTW, though, the xhr.request?, which I discovered in this process, is helpful for detecting javascript calls, and preventing certain actions from responding to javascript.

A: 

This jquery javascript was triggering the extra call:

$.getScript(correct_url, function({
      $.ajax({beforeSend: function(){}...)} 
      callback code
)}

You can't use the ajax shortcuts inside the getScript shorthand function like that. The inner .ajax was making its own call. So I simply combined them into one .ajax function

$.ajax({
   url: correct_url,
   type: 'get',
   dataType: 'script',
   beforeSend: function(){

       }),
       success: function (){
            <callback code>
       }
})
Michael Waxman