Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在app中使用,不起作用; #1

Open
wangxiaote opened this issue Jan 18, 2017 · 9 comments
Open

在app中使用,不起作用; #1

wangxiaote opened this issue Jan 18, 2017 · 9 comments

Comments

@wangxiaote
Copy link

我在APP中写了一个webview,在其中加载网页;网页中有一个ajax请求,用于请求网页中需要显示的数据;这个数据我不想用户过于频繁的获取,一方面也不想缓存过久,所以就用了ajax-cache来做;但是看起来,每次请求都发出去了
$.ajax({
ajaxCache: {
cacheValidate: function (res, options) {
return true;
},
storageType: 'localStorage',
timeout: 30,
forceRefresh: false
},
type:"get",
url: "http:xxxxxx.xxxx.com",
async: true,
dataType:"json",
success:function(data) {

				}
			});
@wuchangming
Copy link
Member

在一般的浏览器运行该逻辑有没效果呢?

@wangxiaote
Copy link
Author

看起来也没有作用;
我尝试定位了下,下面这个接口看起来没有调用
cacheValidate: function (res, options) {
return true;
},

@wuchangming
Copy link
Member

1、你用的什么版本的jquery?

2、是不是按照这个顺序引入的?

<script src="../node_modules/jquery/dist/jquery.js"></script>
<script src='../dist/jquery-ajax-cache.min.js'></script>

@wangxiaote
Copy link
Author

<script language="JavaScript" src="js/jquery-1.9.1.min.js"> </script>
<script language="JavaScript" src="js/jquery-ajax-cache.js"></script>

@wuchangming
Copy link
Member

全局需要先调用下 $ajaxCache.config();

参考下面代码:

<!DOCTYPE html>
<html>

<head>
    <title>test jquery-ajax-cache</title>

    <head>
        <script  src="js/jquery-1.9.1.min.js"></script>
        <script  src="js/jquery-ajax-cache.js"></script>
        <script>

            $ajaxCache.config();

            $.ajax({
                ajaxCache: {
                    cacheValidate: function(res, options) {
                        return true;
                    },
                    storageType: 'localStorage',
                    timeout: 30,
                    forceRefresh: false
                },
                type: "GET",
                url: "https://api.github.com/repos/WQTeam/jquery-ajax-cache",
                dataType: "json",
                success: function(data) {
                    console.log(data);
                }
            })
        </script>

</html>

@wangxiaote
Copy link
Author

增加了$ajaxCache.config();之后,在PC上是可以了。
但是在APP里还是不行。
PS:我在设置webview参数的时候,并没有打开cache,因为网页本身我不希望受到缓存的影响,这个有关系吗?

@wuchangming
Copy link
Member

@wangxiaote ,应该有关系的。你打开cache试试。

@wangxiaote
Copy link
Author

        var cacheProxy = $ajaxCache.getCacheProxy();

        var ajaxCacheOptions = options.ajaxCache;

        if (ajaxCacheOptions) {
            var storage = cacheProxy.getStorage(ajaxCacheOptions.storageType);

            if (!storage.isSupported()) {
                return;
            }

调试的结果,storage.isSupported()是false;
现在APP中的webview设置如下:
webSettings.setAppCacheEnabled(true);
webSettings.setAppCachePath(this.getContext().getCacheDir().getAbsolutePath());
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

@wangxiaote
Copy link
Author

storageType: 'sessionStorage' 这样就可以了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants