【问题标题】:how to store in phonegap from ajax to localStorage如何在phonegap中从ajax存储到localStorage
【发布时间】:2017-09-18 04:36:06
【问题描述】:

我们有一个与 cordova 配合使用的应用程序 [phonegap] 我们希望将结果离线保存到缓存中

结果来自服务器 [PHP] 而不是使用 webview 中显示的 ajax 和 javascript 结果

当用户处于离线模式 [无 wifi/3G] 时,我尝试使用 localStorage 来保留上次使用的值 [2]。美元/欧元或美元/ILS,即

使用 jQuery 正常工作

        $('.loading').fadeOut();

        var $response           = $(result);
        var result              = $response.filter('#result').html();
        var result_1            = $response.filter('#result_1').html();
        var result_10           = $response.filter('#result_10').html();
        var result_100          = $response.filter('#result_100').html();
        var result_1000         = $response.filter('#result_1000').html();

        $('.convertfrom_txt').each(function() { $(this).text(cfrom_str); })
        $('.convertto_txt').each(function() { $(this).text(cto_str); })

        $('#cresult').text(result + ' ' + cto_str);
        $('#one_unit').text(result_1);
        $('#ten_unit').text(result_10);
        $('#hundred_unit').text(result_100);
        $('#thousand_unit').text(result_1000);

离线假设价值每天都在变化。 [货币]

var currency = [10]; //10 currencies max to save offline

var currency = result_1 ; //save result of 1 coin to array

var offline;
//Check connection
function onDeviceReady() {
    var offlineData = window.localStorage.getItem("result_1");

    if (window.navigator.onLine) offline = false;
}

if (offline == false) 
    {
        document.getElementById('result_1').value = "currency[0]";
        document.getElementById('result_10').value = "currency[0]*10";
        document.getElementById('result_100').value = "currency[0]*100";
        document.getElementById('result_1000').value = "currency[0]*1000";
        document.getElementById('time').value = "time";
    }

result_1 表示一次转化的价值。 我们希望将 1 美元和 1 欧元的值存储在一个数组中,离线

我不确定选择哪种更好的方法

localStorage 采用官方形式 Docs

var storage = localStorage;

var value = storage.getItem(key); // Pass a key name to get its value.

storage.setItem(key, value) // Pass a key name and its value to add or update that key.

storage.removeItem(key) // Pass a key name to remove that key from storage.
  1. 这样的本地存储能否在常见的 Android 设备下工作?

谢谢

【问题讨论】:

  • 查看更多信息here
  • 您的问题是什么?如何store array in local storage 或什么?
  • 是的 Pavlo,您实际上已经在我的问题中添加了一个链接
  • 查看附加的问题链接如何实现它

标签: jquery ajax html cordova local-storage


【解决方案1】:

LocalStorage 受所有 Cordova 平台支持,包括Android。来自docs的信息。

如何在LocalStorage中存储数组描述here

【讨论】:

  • 我使用的是数字(3.5556)我需要一个数组还是应该将它用作字符串。谢谢参考
  • 据我所知,您可以使用 JSON.parse(localStorage.getItem(yourStorageKey)) 来更正来自LocalStorage的解析号
猜你喜欢
  • 2015-03-01
  • 2017-04-27
  • 1970-01-01
  • 2011-12-10
  • 2011-09-24
  • 1970-01-01
  • 2014-02-18
  • 2011-03-06
相关资源
最近更新 更多