javascript 我可以使用 AJAX 'POST' 将数据发布到我服务器上的 JSON 文件吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30285124/
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
Can I use AJAX 'POST' to post data to a JSON file on my server?
提问by user3648969
I just want the easiest/most simple way to get my data from an AJAX form using 'POST' the data the user entered on my server.
我只想要最简单/最简单的方法来使用“POST”用户在我的服务器上输入的数据从 AJAX 表单中获取我的数据。
So if the user leaves their name in the input form on the page, then AJAX POST's the data to a JSON file on my server.
因此,如果用户在页面上的输入表单中留下他们的名字,那么 AJAX POST 会将数据发送到我服务器上的 JSON 文件中。
Is this possible? Is this the quickest way to get the data that is entered?
这可能吗?这是获取输入数据的最快方法吗?
Thanks in advance!
提前致谢!
*can someone tell me why this got downvoted? Am I violating any terms? I would just like to know in the future. Thanks. :/
*有人可以告诉我为什么这被否决了吗?我是否违反了任何条款?我只是想知道将来。谢谢。:/
回答by Karthik
Ajax file directly can not write to a file in your server. But you can achieve this by creating simple phpscript say savejson.php
on your server.
Your form:
Ajax 文件不能直接写入服务器中的文件。但是您可以通过在您的服务器上创建简单的 php脚本来实现这一点savejson.php
。
您的表格:
<form>
<input type="text" id="name" name="name">
<button type="submit" id="submit-btn">
</form>
<script>
$('#submit-btn').on('click', function(e) {
e.preventDefault();
if( $('#name').val() ){
$.ajax({
url : 'savejson.php',
method : 'post',
data : { 'name': $('#name').val() },
success : function( response ) {
alert( response );
}
});
}
});
</script>
Your savejson.php:
你的 savejson.php:
<?php
$fp = fopen('names.json', 'w');
fwrite($fp, json_encode($_POST['name']));
fclose($fp);
?>
回答by Quentin
Ajax is a term that more-or-less just means "Make an HTTP request from JavaScript".
Ajax 是一个术语,或多或少的意思是“从 JavaScript 发出 HTTP 请求”。
You can use it to make a POST request. You can use it to make the body of that request a JSON text.
您可以使用它来发出 POST 请求。您可以使用它使该请求的正文成为 JSON 文本。
You can't POST to a file, only to a URL. You would need server side code that would be responsible for taking the data in the request and writing it to a file.
您不能发布到文件,只能发布到 URL。您将需要负责获取请求中的数据并将其写入文件的服务器端代码。
回答by Huey
Yes. This is a rather common question and you'd do good to take a look at the following questions as well:
是的。这是一个相当常见的问题,您最好也看看以下问题:
How can I use JQuery to post JSON data?
Jquery Ajax Posting json to webservice
Jquery Ajax 将 json 发布到 webservice
Essentially, you use a client-side language (Javascript) to send a POST request to your backend. Naturally, you will then require a backend language (such as PHP or node.js).
本质上,您使用客户端语言 (Javascript) 向后端发送 POST 请求。自然地,您将需要一种后端语言(例如 PHP 或 node.js)。
I'll provide an example:
我将提供一个例子:
JS (jQuery):
JS(jQuery):
$.post("http://yourURL.com/backend.php",{
data: {
"name" :"John Smith",
"vehicle":"TARDIS"
}
}).success(function(response){
console.log(response)
//do something with the response
});
PHP
PHP
<?php
$data = json_decode($_POST['data'], true);
$name = $data['name'];
$vehicle = $data['vehicle'];
echo "Welcome {$vehicle}-driving $name!";
?>
Your PHP especially should include error checking among other things, but this will suffice for a simple example.
您的 PHP 尤其应该包括错误检查等功能,但这对于一个简单的示例就足够了。
In your console, upon executing the AJAX request you will then see the following:
在您的控制台中,执行 AJAX 请求后,您将看到以下内容:
Welcome TARDIS-driving Doctor!
Which is the output from your PHP file
这是您的 PHP 文件的输出
回答by Ans
Sorry to jump in late here.
抱歉在这里迟到了。
var name = $('#name').val();
$.ajax({
url : 'somefile.php',
method : 'post',
data : { 'name': name },
success : function( response ) {
console.log( response );
}
});
This will do the trick.
这将解决问题。
回答by Riccardo Rich Giacomelli
You cannot write to any file using JavaScript alone. Just cookies or local (or session) storage.
您不能单独使用 JavaScript 写入任何文件。只是 cookie 或本地(或会话)存储。
回答by Priya Ranjan Singh
AJAX POST sends data to server. The data is in JSON format. You understand this part already.
AJAX POST 将数据发送到服务器。数据采用 JSON 格式。这部分你已经明白了。
On server, there needs to be a API to consume this JSON and write it to a file, or better store it in a database.
在服务器上,需要有一个 API 来使用这个 JSON 并将其写入文件,或者更好地将其存储在数据库中。
More questions:
Can it directly write to a file? No
Can API write to the file? Yes, its technically possible but not advisable
How do others do it? They take JSON response and write it to a database in server side code.
更多问题:
可以直接写入文件吗?否
API 可以写入文件吗?是的,技术上可行但不可取
其他人是如何做到的?他们采用 JSON 响应并将其写入服务器端代码中的数据库。