0

I am trying to cache a very simple javascript response. I am using rails and my views/projects/index.js.erb contains only the following:

          alert('hi');

and when I request

  $.ajax({
    type: 'GET',
    cache: true,
    url : '/projects',
    dataType: 'script'
  });

I get the popup 'hi' and I see in my server log that a request to Projects#index action as js has been made to it.

Then without refreshing the browser and I do it again

  $.ajax({
    type: 'GET',
    cache: true,
    url : '/projects',
    dataType: 'script'
  });

I see that the server still gets a request can anyone spot anything that I might have missed?

Thank you!

nemke
  • 2,440
  • 3
  • 37
  • 57
Nik So
  • 16,683
  • 21
  • 74
  • 108
  • No, no error in the console at all. I checked both firefox and chrome's console. and I am not getting any request logged in the server log; and I have already went ahead and create a new simple app just so that none other plugin/script or anything can be interfereing. just a naked barebone page now with nothing on it. – Nik So Mar 23 '11 at 02:19
  • 2
    Maybe I'm looking at the jQuery docs wrong, but isn't the second param to getScript the callback function? – BStruthers Mar 23 '11 at 02:19
  • @BStruthers: Aha, you are correct. The settings should be passed to `$.ajax()` not `$.getScript()`. That's probably the issue. – Richard Marskell - Drackir Mar 23 '11 at 02:22
  • so should I do $.ajax(url, {dataType:'script',cache:true})? – Nik So Mar 23 '11 at 02:23
  • @Drackir:now it's not cached :( – Nik So Mar 23 '11 at 03:04

1 Answers1

1

I found out what's wrong. jQuery actually surround the incoming script with a so that the browser evaluates the incoming code. But the caching mechansim merely saves the code as text and when one re-request, it returns the code as text but not evaluate it. Therefore, one needs to eval the code explicitly

Nik So
  • 16,683
  • 21
  • 74
  • 108