Can I use AJAX 'POST' to post data to a JSON file on my server?

南笙酒味 提交于 2021-02-07 07:59:33

问题


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.

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.

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. :/


回答1:


Ajax file directly can not write to a file in your server. But you can achieve this by creating simple php script say savejson.php on your server.
Your form:

<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:

<?php
    $fp = fopen('names.json', 'w');
    fwrite($fp, json_encode($_POST['name']));
    fclose($fp);
?>



回答2:


Ajax is a term that more-or-less just means "Make an HTTP request from JavaScript".

You can use it to make a POST request. You can use it to make the body of that request a JSON text.

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.




回答3:


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

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).

I'll provide an example:

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
    $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.

In your console, upon executing the AJAX request you will then see the following:

Welcome TARDIS-driving Doctor!

Which is the output from your PHP file




回答4:


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.




回答5:


You cannot write to any file using JavaScript alone. Just cookies or local (or session) storage.




回答6:


AJAX POST sends data to server. The data is in JSON format. You understand this part already.

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.

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.



来源:https://stackoverflow.com/questions/30285124/can-i-use-ajax-post-to-post-data-to-a-json-file-on-my-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!