使用 JQuery 简单保存到 JSON 文件 [英] Simple save to JSON file with JQuery

查看:62
本文介绍了使用 JQuery 简单保存到 JSON 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了所有可以使用的示例,但我无法简单地将 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>

使用 myscript.js 中的 id 选择器,我这样做:

With id selector in a myscript.js I do this:

$('#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.

$.ajax 部分在我上面的代码中没有任何作用.甚至没有警告错误".其余代码按预期工作.

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.

如果有完整示例的帮助或有用的链接,我将不胜感激.谢谢!

推荐答案

$.ajax 单独保存 json 文件,需要将 url 属性指向一个服务器端脚本,即 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:

<?php
$myFile = "general.json";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_GET["data"];
fwrite($fh, $stringData);
fclose($fh)
?>

您还需要将 ajax 调用中的 data 属性更改为 data: {data: 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:

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!");}
    });

这篇关于使用 JQuery 简单保存到 JSON 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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