使用 JQuery 简单保存到 JSON 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20948155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple save to JSON file with JQuery
提问by Laci
I've tried out all examples I could get my hands on, yet I can't simply save JSON data to a JSON file on my host. I want to start with a simple as possible save method so I have a place to start from.
我已经尝试了所有可以使用的示例,但我不能简单地将 JSON 数据保存到主机上的 JSON 文件中。我想从一个尽可能简单的保存方法开始,这样我就有了一个起点。
Here's what I got:Basically I have a button in my index.htmlwhich on click should save data to my general.json file (same location as index.html).
这就是我得到的:基本上我的index.html 中有一个按钮,单击该按钮会将数据保存到我的 general.json 文件(与 index.html 相同的位置)。
<button id="savebtn">Save</button>
With id selector in a myscript.jsI do this:
使用myscript.js 中的id 选择器,我这样做:
$('#savebtn').click(function() {
var saveit = $('#calendar').fullCalendar( 'clientEvents');
var eventsholded = [];
$.each(saveit, function(index,value) {
var event = new Object();
event.id = value.id;
event.start = value.start;
event.end = value.end;
event.title = value.title;
event.allDay = value.allDay
eventsholded.push(event);
});
$.ajax
({
type: "GET",
dataType : 'json',
async: false,
url: 'general.json',
data: JSON.stringify(eventsholded),
success: function () {alert("Thanks!"); },
failure: function() {alert("Error!");}
});
As you can see I want to store events from fullcalendar. That is not very relevant because it works fine till this point. If I alert on screen JSON.stringify(eventsholded)you will see this:
如您所见,我想从 fullcalendar 存储事件。这不是很重要,因为到目前为止它工作正常。 如果我在屏幕 JSON.stringify(eventsholded) 上发出警报,您将看到:
[{"start":"2014-01-07T08:30:00.000Z","end":"2014-01-07T12:30:00.000Z","title":"Pumukli Pista","allDay":false},{"start":"2014-01-11T13:30:00.000Z","end":"2014-01-11T18:30:00.000Z","title":"Fanic Catalin","allDay":false}]
Now this is exactly what I want to save to server in simple, quick, maybe unsecure, but very simple way.Just so I can start to understand how this works, just to have that in my general.json file.
现在这正是我想以简单、快速、可能不安全但非常简单的方式保存到服务器的内容。只是为了让我开始了解这是如何工作的,只是为了在我的 general.json 文件中包含它。
The $.ajax
part does nothing in my above code. Not even alerting "Error". The rest of the code works as expected.
该$.ajax
部分在我上面的代码中没有任何作用。甚至没有警告“错误”。其余代码按预期工作。
Security is not important now.I just want to learn how it works.
现在安全不重要。我只是想了解它是如何工作的。
I will be grateful for any help or useful links that have complete examples. Thank you!
我将不胜感激任何有完整示例的帮助或有用的链接。谢谢!
回答by Yeray Diaz Diaz
$.ajax
alone will not save the json file, you need to direct the url
property to a server-side script, i.e. http://your.host/save_json.php
, that will create general.json
and write your output on it. Something like:
$.ajax
单独不会保存 json 文件,您需要将该url
属性定向到服务器端脚本,即http://your.host/save_json.php
,它将general.json
在其上创建和写入您的输出。就像是:
PHP:
PHP:
<?php
$myFile = "general.json";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_GET["data"];
fwrite($fh, $stringData);
fclose($fh)
?>
You'll also need to change the data
property in your ajax
call to data: {data: JSON.stringify(eventsholded)}
to give the GET variable a proper name that can be retrieved from PHP:
您还需要更改调用中的data
属性,以便为 GET 变量提供一个可以从 PHP 检索的正确名称:ajax
data: {data: JSON.stringify(eventsholded)}
JQUERY
查询
$.ajax
({
type: "GET",
dataType : 'json',
async: false,
url: 'http://your.host/save_json.php',
data: { data: JSON.stringify(eventsholded) },
success: function () {alert("Thanks!"); },
failure: function() {alert("Error!");}
});