jquery script request not caching
Asked Answered
F

1

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!

Fourpence answered 23/3, 2011 at 2:9 Comment(5)
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.Fourpence
Maybe I'm looking at the jQuery docs wrong, but isn't the second param to getScript the callback function?Proudhon
@BStruthers: Aha, you are correct. The settings should be passed to $.ajax() not $.getScript(). That's probably the issue.Extramarital
so should I do $.ajax(url, {dataType:'script',cache:true})?Fourpence
@Drackir:now it's not cached :(Fourpence
F
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

Fourpence answered 25/3, 2011 at 8:0 Comment(1)
surround the incoming script with a what?Fatherly

© 2022 - 2024 — McMap. All rights reserved.