jQuery脚本请求不缓存 [英] jquery script request not caching

查看:100
本文介绍了jQuery脚本请求不缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试缓存一个非常简单的javascript响应.我正在使用Rails,而我的views/projects/index.js.erb仅包含以下内容:

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');

并且在我请求时

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

我得到弹出消息"hi",我在服务器日志中看到对js的Projects#index动作的请求.

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?

谢谢!

推荐答案

我发现了问题所在. jQuery实际上用括住传入脚本,以便浏览器评估传入代码.但是缓存机制仅将代码保存为文本,当重新请求时,它将代码返回为文本,但不对其进行求值.因此,需要明确评估代码

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

这篇关于jQuery脚本请求不缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆