javascript 使用 jquery 将表单数据保存到本地 json 文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13107567/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:49:56  来源:igfitidea点击:

Save form data into local json file using jquery

javascriptjsonformsjquery

提问by Alonso

I have a basic form with some input fields. I want to save the form data into a json file on submitting the form. The format of the saved data in the json file should be like this.

我有一个带有一些输入字段的基本表单。我想在提交表单时将表单数据保存到 json 文件中。json文件中保存的数据格式应该是这样的。

[
    {"title":"some text","description":"some text","info":"some text","username":"some name"},
    {"title":"some text","description":"some text","info":"some text","username":"some name"},
    {"title":"some text","description":"some text","info":"some text","username":"some name"}
]

I tried doing this by using this code, but no data is saved in my 'story.json file'

我尝试使用此代码执行此操作,但我的“story.json 文件”中未保存任何数据

$('form').submit(function() {
    $.ajax({
       type: 'POST', 
       dataType: 'json', 
       url: 'story.json', 
       data: $(this).serialize(), 
       success: function(data) { 
           alert(data.message); 
       },
       failure: function (data) {
           alert('Please try again');
       }
    });
 });

I want to save this form data on my local file. Please help me to find the correct way of doing this. Thanks

我想将此表单数据保存在我的本地文件中。请帮我找到正确的方法。谢谢

采纳答案by StaticVariable

You need to post data to a simple php file...

您需要将数据发布到一个简单的 php 文件...

like url: 'story.php'In that php file create a 'story.json' using fopenand store that json

就像url: 'story.php'在那个 php 文件中创建一个 'story.json' 使用fopen并存储该 json

EDIT: if you want to use serialize()than use someting like this

编辑:如果你想使用serialize()比使用这样的东西

data:{'mydata':$(this).serialize()}

and in php file

并在 php 文件中

parse_str($_POST['mydata'],$newarray) ;

echo json_encode($newarray);

回答by HMR

JavaScript cannot save to a file unless it's a FireFox plugin.

JavaScript 无法保存到文件,除非它是 FireFox 插件。

What you do is post a form (sent it to the webserver) then let server side script handle it.

您所做的是发布一个表单(将其发送到网络服务器),然后让服务器端脚本处理它。

Serialize does not turn the form values into a JSON string:

Serialize 不会将表单值转换为 JSON 字符串:

http://api.jquery.com/serialize/

http://api.jquery.com/serialize/

And when you use $.post then you have to return false in the $('form').submit(function() or the browser will submit the form for you.

当您使用 $.post 时,您必须在 $('form').submit(function() 中返回 false,否则浏览器将为您提交表单。

Submitting a form is when you click a button and the whole page goes white for a moment and then you get a new page.

提交表单是指当您单击一个按钮时,整个页面会变成白色,然后您会看到一个新页面。

Ajax ($.post, $.get, $.getJson ...) is when you send information to the server without refreshing the page. Google maps is an excellent example.

Ajax ($.post, $.get, $.getJson ...) 是指您在不刷新页面的情况下向服务器发送信息。谷歌地图就是一个很好的例子。

回答by Asad Saeeduddin

Calling the serialize method on the form jQuery object does not return a JSON representation of its data. It returns a querystring representation of its data. As has been mentioned above, you will need to use a server side script to interpret (and manipulate) sent data and store it in a file as JSON.

在表单 jQuery 对象上调用 serialize 方法不会返回其数据的 JSON 表示。它返回其数据的查询字符串表示。如上所述,您需要使用服务器端脚本来解释(和操作)发送的数据并将其作为 JSON 存储在文件中。