我的网络应用程序应该能够在服务器上存储和更新(也加载)JSON数据。 但是,数据可能包含一些大数组,每次保存时只会附加一个新条目。
使用json数据中的密钥路径向服务器发送更新。
目前我正在通过jquery发送带有xmlhttprequest的数据,比如
/**
* Asynchronously writes a file on the server (via PHP-script).
* @param {String} file complete filename (path/to/file.ext)
* @param content content that should be written. may be a js object.
* @param {Array} updatePath (optional), json only. not the entire file is written,
* but the given path within the object is updated. by default the path is supposed to contain an array and the
* content is appended to it.
* @param {String} key (optional) in combination with updatePath. if a key is provided, then the content is written
* to a field named as this parameters content at the data located at the updatePath from the old content.
*
* @returns {Promise}
*/
io.write = function (file, content, updatePath, key) {
if (utils.isObject(content)) content = JSON.stringify(content, null, "\t");
file = io.parsePath(file);
var data = {f: file, t: content};
if (typeof updatePath !== "undefined") {
if (Array.isArray(updatePath)) updatePath = updatePath.join('.');
data.a = updatePath;
if (typeof key !== "undefined") data.k = key;
}
return new Promise(function (resolve, reject) {
$.ajax({
type: 'POST',
url: io.url.write,
data: data,
success: function (data) {
data = data.split("\n");
if (data[0] == "ok") resolve(data[1]);
else reject(new Error((data[0] == "error" ? "PHP error:\n" : "") + data.slice(1).join("\n")));
},
cache: false,
error: function (j, t, e) {
reject(e);
//throw new Error("Error writing file '" + file + "'\n" + JSON.stringify(j) + " " + e);
}
});
});
};
在服务器上,php脚本管理其余部分:
然而,我并不是很开心,问题在最后几周仍然存在。
答案 0 :(得分:1)
也许你可以重用我用C ++和CoffeeScript编写的一些代码:https://github.com/TorstenRobitzki/Sioux