我已经尝试了所有的例子我能得到我的手,但我不能JSON数据只需保存到我的主机上的JSON文件。我想先从简单地保存方法,所以我有一个地方的开始。
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.
这是我得到的答案: 基本上我有一个按钮,在我的 index.html的这对点击应该将数据保存到我的general.json文件(同一位置的index.html)。
Here's what I got: Basically I have a button in my index.html which on click should save data to my general.json file (same location as index.html).
<button id="savebtn">Save</button>
随着id选择一个 myscript.js 我这样做:
$('#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!");}
});
正如你可以看到我想要存储的fullcalendar事件。这是不是很重要,因为它工作得很好,直到这一点。 如果我提醒屏幕上JSON.stringify(eventsholded)您会看到这样的:
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:
[{"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}]
现在的这正是我要保存到服务器的简单,快捷,也许不安全,但很简单的方式。只是这样我就可以开始了解如何工作的,只是有在我general.json文件。
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.
在 $。阿贾克斯
部分不做任何事情在我上面的code。甚至没有报警的错误。的code,其余按预期工作。
The $.ajax
part does nothing in my above code. Not even alerting "Error". The rest of the code works as expected.
安全性不重要了。我只是想了解它是如何工作的。
Security is not important now. I just want to learn how it works.
我会很感激的任何帮助或有完整的例子有用的链接。谢谢!
$。阿贾克斯
不会单独保存JSON文件,则需要直接网址
属性设置为一个服务器端脚本,即 http://your.host/save_json.php
,这将创建 general.json
,写你的输出就可以了。是这样的:
$.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:
<?php
$myFile = "general.json";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_GET["data"];
fwrite($fh, $stringData);
fclose($fh)
?>
您还将需要更改数据
属性的 AJAX
致电数据:{数据:JSON.stringify(eventsholded)}
给GET变量,可以从PHP检索一个恰当的名称:
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:
$.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!");}
});