HTML 5缓存清单是否也适用于ajax请求? [英] Should HTML 5 cache manifest work with ajax requests too?

查看:160
本文介绍了HTML 5缓存清单是否也适用于ajax请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ASP MVC 3网站获取HTML 5离线应用程序缓存。我得到的问题是
当我尝试在离线模式下导航到页面时,它不起作用。

I'm trying to get HTML 5 offline application cache working with an ASP MVC 3 website. The problem I get is that when I try to navigate to a page in offline mode, it doesn't work.

我正在使用一个动作清单文件,以便它可以动态生成,并在视图中我指定
Resonse.ContentType =text / cache-manifest。

I am using an action for the manifest file so that it can be dynamically generated, and in the view I specify the Resonse.ContentType = "text/cache-manifest".

我有在IIS本地托管应用程序,所以我使用 http://192.168.55.127/mywebsite/ 来访问它。

I have hosted the application locally in IIS so I'm using http://192.168.55.127/mywebsite/ to access it.

这是我正在使用的清单视图。它使用剃刀视图引擎并且有点乱(硬编码的URL等)
而我试图弄清楚什么是错的。

This is the manifest view I'm using. It uses the razor view engine and is a bit messy (hard coded URL etc) while I try to figure out what's wrong.

@{
     Layout = null;
     Response.ContentType = "text/cache-manifest";
}
CACHE MANIFEST

# Version: @ViewBag.Version

CACHE:
#Script Files
@foreach(var jsFile in Url.GetJsFiles())
{
     @string.Format("{0}{1}\r\n", "http://192.168.55.127", Url.Content(jsFile))
}

#Style Sheets
@foreach(var cssFile in Url.GetCssFiles())
{
     @string.Format("{0}{1}\r\n", "http://192.168.55.127", Url.Content(cssFile))
}

#Images
@foreach(var imageFile in Url.GetImageFiles())
{
     @string.Format("{0}{1}\r\n", "http://192.168.55.127", Url.Content(imageFile))
}

#HTML Pages
@string.Format("{0}{1}", "http://192.168.55.127", Url.Content("~/pages/master.htm"))
@string.Format("{0}{1}", "http://192.168.55.127", Url.Content("~/pages/home.htm"))
@string.Format("{0}{1}", "http://192.168.55.127", Url.Content("~/pages/options.htm"))

NETWORK:
*

这导致路径s uch as:

This results in paths such as:

http://192.168.55.127/mywebsite/scripts/Libs/jQuery.js
http://192.168.55.127/mywebsite/pages/home.htm

这似乎没问题。

我也使用完整路径引用了清单文件:

I have referenced the manifest file using the full path too:

<html manifest="http://192.168.55.127/mywebsite/manifest">

这似乎没问题,就像我在chrome中加载网站并观察开发者控制台一样看起来
来缓存所有文件而不会抛出任何错误。此外,如果我导航到 http://192.168.55.127/mywebsite/manifest
它还可以提供服务我期望看到它的清单。

which seems to be ok, as when I load the site up in chrome and observe the developer console, it appears to cache all the files without throwing any errors. Also if I navigate to http://192.168.55.127/mywebsite/manifest it serves up the manifest as I'd expect to see it.

该网站不使用普通导航,而是使用散列片段导航 - 所以要导航到家,网址会是master.htm #home或者选项是master.html#options。这个哈希更改由javascript拾取,它使用ajax将页面加载到master中的div容器中,更具体地说,它使用jQuery中的'load'方法来执行此操作。

The website doesn't use normal navigation, instead it navigates using hash fragments - so to navigate to home the url would be master.htm#home or for options it would be master.html#options. This hash change is picked up by javascript and it loads the page into a div container in the master using ajax, more specifically it uses the 'load' method in jQuery to do this.

这一切在不处于离线模式时工作正常,并且在导航时观察chrome中的网络选项卡时,请求URL是正确的,并且与清单文件中列出的URL相同。我唯一能想到的是离线模式不适用于ajax请求,但我的印象是它的工作原理相同。

This all works fine when not in offline mode, and when observing the network tab in chrome when navigating, the request URL is correct and is the same URL that is listed in the manifest file. The only thing I can think of is that offline mode doesn't work for ajax request, but I was under the impression that it worked the same.

我正在离线测试通过清除所有历史记录,浏览到网站主页,启用离线模式,然后尝试导航到选项页面,使用FireFox(版本9.0)模式。在firebug中,我看到GET请求选项页面的正确URL,但它永远不会返回,甚至不会出错。加载轮(在firebug中的网络选项卡中的请求旁边)只是一直在转动,好像它仍然在加载。我也在Opera 11.60中尝试过它(因为它也有离线模式)并且发生同样的事情。

I am testing offline mode using FireFox (version 9.0) by clearing down all history, browsing to the website home page, enabling offline mode, then trying to navigate to the options page. In firebug I see a GET request for the correct URL of the options page but it never returns, it doesn't even error. The loading wheel (next to the request in the net tab in firebug) just keeps turning as if it is still loading. I tried it in Opera 11.60 too (as that also has an offline mode) and the same kind of thing happens.

任何人都对我的想法有任何想法做错了?我是否错过了明显或误解清单应如何工作的东西?任何建议都将不胜感激。

Any one have any ideas as to what I'm doing wrong? Have I missed something obvious or misunderstood how the manifest should work? Any suggestions will be appreciated.

推荐答案

(我知道这个问题已经过去了,但供将来参考......)

(I know the question's old but for future reference...)

如果AJAX内容文件在AppCache清单文件中正确列出(它们似乎是),那么这应该可行。就个人而言,我会使用相对路径而不是绝对路径,但这不应该有所作为。

If the AJAX content files are listed in the AppCache manifest file properly (which they seem to be) then this should work. Personally, I'd use relative rather than absolute paths but that shouldn't make a difference.

您的问题似乎是清单文件没有文件延期。尝试将文件(及其在master.htm中的引用)重命名为 appcache.manifest 或类似文件。然后,您需要确保在服务器中设置清单文件的MIME类型。例如。对于Apache你会添加如下内容:

Your problem seems to be that the manifest file doesn't have a file extension. Try renaming the file (and its reference in master.htm) to appcache.manifest or similar. Then you need to make sure the manifest file's MIME type is set in the server. E.g. for Apache you'd add something like:

AddType text/cache-manifest .manifest

到服务器的配置文件或.htaccess文件。

to the server's config file or your .htaccess file.

此外,还有在测试时清除缓存的数据,确保在更改清单文件时至少刷新页面几次,因为浏览器会检查更新并在单独的页面加载中下载文件。

Also, as well as clearing cached data when testing, make sure you refresh the page at least a couple of times when you make a change to the manifest file because the browser checks for updates and downloads files in separate page loads.

最后,如果你用AJAX引入的文件在URL中有参数,它将无法工作,例如?id = 1234,但未在清单文件中列出。这似乎不是这里的情况,但需要注意的是。

Finally, it won't work if the files you're pulling in with AJAX have parameters in the URL, e.g. ?id=1234, but are not listed as such in the manifest file. That doesn't seem to be the case here but it's something to be aware of.

这篇关于HTML 5缓存清单是否也适用于ajax请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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