【问题标题】:Android javascript cookie doesn`t work in webviewAndroid javascript cookie 在 webview 中不起作用
【发布时间】:2018-11-01 11:53:26
【问题描述】:

我正在尝试使用 WebView 编写 android 应用程序。当我使用 javascript 从 webView 设置 cookie 时,它​​不起作用。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView webView = (WebView) findViewById(R.id.webview);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    CookieManager.getInstance().setAcceptCookie(true);
    webSettings.setDomStorageEnabled(true);
    webView.loadUrl("file:///android_asset/index.html");
}

index.html 代码:

<html>
    <head>
        <!--Meta-->
        <meta charset="utf-8">
    </head>
    <body>
        <script>
            document.cookie = "userName=Vasya";
            console.log("cookies:");
            console.log(document.cookie);
        </script>
    </body>
</html>

控制台输出:

11-01 17:35:16.239 26866-26866/? I/chromium: [INFO:CONSOLE(9)] "cookies:", source: file:///android_asset/index.html (9)
11-01 17:35:16.242 26866-26866/? I/chromium: [INFO:CONSOLE(10)] "", source: file:///android_asset/index.html (10)

【问题讨论】:

标签: javascript android cookies webview


【解决方案1】:

我通过使用 localStorage 解决了这个问题 localStorage.setItem('foo', 'bar'); localStorage.getItem('foo')

【讨论】:

    猜你喜欢
    • 2015-03-22
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 2014-07-31
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    相关资源
    最近更新 更多