【问题标题】:window.localStorage.setItem not working on mobile phonewindow.localStorage.setItem 在手机上不起作用
【发布时间】:2015-05-26 09:17:59
【问题描述】:

我正在使用 jQuery mobile 开发 HTML5 移动应用程序。

这是我的代码:

$(document).on("click","#send_mobile_number",function(){

    var mobile = $('#mobile_number').val();
    var user_id = sessionStorage.getItem("user_id");


    $('.ui-loader').show();

    $.ajax({
        url: BASE_URL+'users/send_sms_code.php',
        type: 'POST',
        datatype: 'json',
        data: "user_id="+user_id+"&mobile="+mobile+"&type=1",
        async:false,
        success: function (response) {
            var data = jQuery.parseJSON(response);
            $('.ui-loader').hide();

            if(data.status == 'Fail') {
                $('.very_mob_no_message').html('Sorry some error occurred,try again.');
            }else{                   
                $('#close_mob_popup').trigger('click');

                setTimeout(function()
                {
                    $('.click_mobile_verify').trigger('click');
                }, 500);

                $('#send_mobile_verify_span').hide();
                $('#after_mobile_send_span').show();

                $('#moble_number_div').hide();
                $('#user_code_div').show();

                $('#user_code').val(data.sms_code);

                //alert(window.localStorage.getItem('mobile'));

                //sessionStorage.setItem("mobile",mobile);
                window.localStorage.setItem("mobile",mobile); // IT IS NOT WORKING


                $('.very_mobile_message').html('Enter code which is <br/> sent to your mobile number.');

            }


        },
        error: function (jqXHR, textStatus, errorThrown) {

            //alert(jqXHR.status);
        }
    });  

});

我想使用window.localStorage.setItem("mobile",mobile); 在会话中存储手机号码。当我在浏览器上运行时它正在工作,但是当我在手机上作为 APP 运行时它停止工作。为什么会这样。我正在检查安卓手机。

【问题讨论】:

  • if(data.status == 'Fail')success 函数中出了点问题...根据定义,如果失败则不会触发成功函数。
  • 进入成功循环但本地存储不工作
  • “localStorage 不工作”已经够了。 localStorage 是一个浏览器功能,它可以工作。警报测试失败(其他 cmets),因此未达到警报,因此问题出在其他地方。查看您的控制台是否有错误,尝试 console.log 内容,例如 console.log(response)console.log(data),并查看它们是否已到达,以及它们输出什么。

标签: android jquery html jquery-mobile local-storage


【解决方案1】:

只需使用localStorage.mobile = "mobile"。就这么简单。 localStorage 是一个全局对象,可以像任何其他对象一样访问和操作。与普通对象的唯一区别是它只能存储字符串。

然后您可以使用alert( localStorage.mobile ); // will alert "mobile"检索您的值

【讨论】:

  • 这里面的value和key是什么?
  • 像任何对象一样:object.key = value。或object['key'] = value.
  • 好的,试试这个代码:localStorage.mobile = "mobile" ; alert(localStorage.mobile)。它会发出什么警报?
  • 我也试过了,但它没有提醒任何东西。它停止工作
  • 所以问题不在于localStorage...您做出了错误的诊断。尝试设置.error() 语句,而不是在success 函数中编写if(data.status == 'Fail')。观察您的控制台是否有错误。有什么错误显示在里面吗?
【解决方案2】:

所以终于找到了解决方案,我需要在android代码上设置webSettings.setDomStorageEnabled(true);,并且在这个localstorage完美运行之后。

【讨论】:

    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多