简体   繁体   English

jQuery Mobile AJAX发布数据未保存到localStorage

[英]Jquery Mobile AJAX post data not saving to localStorage

I have a jquery mobile web app with various pages using single page architecture (one page per html file -- I have multiple HTML files). 我有一个jquery移动网络应用程序,它具有使用单页体系结构的各个页面(每个HTML文件一个页面-我有多个HTML文件)。 "page1" is the originally loaded page. “ page1”是最初加载的页面。 I have 4 other tabs for navigation [page2, page3, page4, and page5] which are all pre-fetched from the menu in page1. 我还有其他四个用于导航的选项卡[page2,page3,page4和page5],这些选项卡都是从page1的菜单中预提取的。

On almost every page, I use AJAX post calls to interact with my application. 在几乎每个页面上,我都使用AJAX帖子调用与我的应用程序进行交互。 For efficiency/usability, I save the content to localStorage. 为了提高效率/可用性,我将内容保存到localStorage。

The code on page1 looks like this: 第1页上的代码如下所示:

$(document).on('pageshow','#page1', function(){ 

    var postTo = '../api/ajaxreturn.php';
    $.post(postTo, function (data){
        if (localStorage.getItem('ajaxreturn') == JSON.stringify(data)) return;
        else {
          localStorage.setItem('ajaxreturn',JSON.stringify(data));
          //alert(localStorage.getItem('ajaxreturn'))

        }
    }, "json");//end post
     //alert('hi')        

}); // end page1

Other pages are coded similarly. 其他页面的编码类似。

The problem I experience is that on the ORIGINAL webapp document load, the information is not being saved to localStorage. 我遇到的问题是,在加载原始Webapp文档时,该信息未保存到localStorage。 When I check in the debugger, I see that all the localStorage data of the prefetched pages is saved. 在调试器中签入时,我看到预取页面的所有localStorage数据都已保存。 In order to get page1's localStorage I have to click to any page2/3/4/5 and then back to page1. 为了获得page1的localStorage,我必须单击到任何page2 / 3/4/5,然后返回到page1。 This calls the "pageshow" function again and it works. 这将再次调用“页面显示”功能,并且可以正常工作。

What is even more weird is that when I uncomment the alert('hi'), IF I leave the alert up for a couple of seconds, the data does show up in localStorage on the ORIGINAL load of the webapp. 更奇怪的是,当我取消注释警报(“ hi”)时,如果我将警报搁置了几秒钟,则数据确实显示在webapp原始负载的localStorage中。

Maybe it has to do with timing issues? 也许与计时问题有关? race conditions? 比赛条件? But I know the ajax call is working, because if I uncomment "alert(localStorage.getItem('ajaxreturn'))" I see the actual content.. So not sure what it is. 但是我知道ajax调用是有效的,因为如果我取消注释“ alert(localStorage.getItem('ajaxreturn'))”,我会看到实际的内容。所以不知道它是什么。

Any help would be greatly appreciated 任何帮助将不胜感激

Fixed it!!! 修复!!!

So the problem had nothing to do with the code I posted. 因此,问题与我发布的代码无关。 It had to do with the order of my header files in the HTML. 它与HTML中我的头文件的顺序有关。

TIP: FOLLOW JQUERY MOBILE GUIDELINES STRICTLY!! 提示:严格遵循JQUERY移动指南!

I had missed the tip where you are supposed to put your custom.js file in between jquery.js and jquery.mobile.js!!! 我错过了提示,提示您应该将custom.js文件放在jquery.js和jquery.mobile.js之间!!!

My previous code looked something like this: 我之前的代码如下所示:

<script src="js/jquery-1.8.2.min.js"></script>
<script src="js/jquery.mobile-1.2.0.min.js"></script>
<script src="js/jquery.swipeButton-1.2.1.js"></script>
<script src="js/ga.js"></script>
<script src="js/custom.js"></script>

When I changed it to the correct way: 当我将其更改为正确的方式时:

<script src="js/jquery-1.8.2.min.js"></script>
<script src="js/custom.js"></script>
<script src="js/jquery.mobile-1.2.0.min.js"></script>
<script src="js/jquery.swipeButton-1.2.1.js"></script>
<script src="js/ga.js"></script>

Notice the placement of custom.js (where all custom script is written for the project). 请注意custom.js的位置(在该位置为项目编写了所有自定义脚本)。

It fixed my problem. 它解决了我的问题。 So definitely make sure your headers are set correctly -- will save you hours and hours of headaches. 因此,请务必确保正确设置标题,这将为您节省数小时的麻烦。 Hopefully this helps you out! 希望这可以帮助您!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM