Jquery发布JSON到本地文件 [英] Jquery posting JSON to local file

查看:84
本文介绍了Jquery发布JSON到本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个PhoneGap应用程序使用jQuery Mobile下载一个JSON文件关于服务器状态和测试目的我使用保存到本地文件夹结构的JSON文件。

I'm working on a Phonegap app using jQuery Mobile to download a JSON file about server statuses and for testing purposes I'm using a JSON file saved into the local folder structure.

我能够使用jQuery $。getJSON 函数轻松地读取文件并显示其数据,但我不确定如何保存数据使用 $。post jQuery函数返回到文件中。

I'm able to read the file and display it's data easily using the jQuery $.getJSON function but I'm unsure as to how to save that data back into the file using the $.post jQuery function.

$。getJSON js / servers.json)可以完美地获得JSON数据(js / servers.json存储在本地文件结构中),但是当我尝试 $。post js / servers.json,jsonServerObj)其中jsonServerObj是我使用 $。getJSON 的数据似乎不是节省

$.getJSON("js/servers.json") Works to get the JSON data perfectly (js/servers.json is stored in the local file structure) but when I try $.post("js/servers.json", jsonServerObj) where jsonServerObj is the JSON I got using $.getJSON the data doesn't seem to be saving to the file properly on page reload.

我的函数:

$.getJSON("js/servers.json").success(function (data)
{
    jsonServerObj = data;
    // Do stuff with data
});

和我的帖子函数:

JSON.stringify(jsonServerObj);

$.post("js/servers.json", jsonServerObj).success(function ()
{
    alert("Data sent!");
})
.error(function ()
{
    alert("UH OH ERROR!");
});

jsonServerObj设置为JS文件的全局变量,并且post函数返回成功警报时间,但是当页面重新加载时,JSON不是应该保存的。

jsonServerObj is set as a global variable for the JS file and the post function is returning the success alert each time,but when the page is reloaded, the JSON is not what should have been saved.

为了参考,我在Android HTC One上运行Android 4.3

For reference, I'm testing this on an Android HTC One running Android 4.3

推荐答案

这是我对自己问题的解决方案,基于意见:

Here's my solution to my own problem, based on comments:

Ok,所以它是不可能发布到本地文件,如一些注释,但通过使用localStorage和 JSON.stringify JSON.parse ,则数据仍然可以在本地存储,如下所示:

Ok, so it isn't possible to post to a local file as mentioned by some of the comments, but by using localStorage and JSON.stringify and JSON.parse as suggested, the data can still be stored locally like so:

function getServerData()
{
    $.getJSON("js/servers.json").success(function (data)
    {
        // Add JSON to localStorage under serverData
        localStorage.setItem("serverData", JSON.stringify(data));
        // Do stuff
    }
}

检索时,我发现您必须先检索数据,然后将其解析为JSON对象,如下所示:

When retrieving, I found that you have to first retrieve the data, then parse it back into a JSON object like so:

function retrieveData()
{
    var temp = localStrage.getItem("serverData");
    var jsonData = JSON.parse(temp);
    // Do stuff with JSON data
}

Phonegap应用程序的localStorage数据是永久的,只要应用程序安装在设备上,即使用户已手动关闭或停止它。

Also, I learned that localStorage data for a Phonegap application is persistent as long as the app is installed on the device, even if it is closed or stopped manually by the user.

我也通过此if / else语句检查数据是否已存在:

I'm also checking to see if the data already exists via this if/else statement:

if (typeof (Storage) != "undefined")
{
    if (!localStorage.getItem("serverData"))
    {
        alert("setting server data");
        // Ajax JSON to get server information
        getServerData();
    }
    else
    {
        retrieveData();
    }
}
else
{
    alert("localStorage unavailable!");
}

此语句通常会为两个ifs返回true,除非应用程序正在打开第一次安装后。我不建议使用localStorage来处理来自非本地Web服务的任何JSON数据,如果该数据可能会被来自此应用程序的输入以外的其他内容更改,因为这只是我原型的应用程序的概念证明。

This statement will usually return true for both ifs unless the app is being opened for the first time after install. I would not recommend using localStorage for any JSON data coming from a non-local web service if that data is likely to be changed by something other than the inputs from this application, as this is only a proof of concept for an application I am prototyping.

这篇关于Jquery发布JSON到本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆