最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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

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

Share Improve this question edited May 17, 2015 at 22:27 h0bb5 asked May 17, 2015 at 8:57 h0bb5h0bb5 5293 gold badges11 silver badges22 bronze badges 1
  • JSON is just a text file, you need to use a server side programming language that receives the data sent by AJAX and store it in a file. – Samurai Commented May 17, 2015 at 9:02
Add a ment  | 

6 Answers 6

Reset to default 7

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);
?>

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.

Yes. This is a rather mon 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./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 "Wele {$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:

Wele TARDIS-driving Doctor!

Which is the output from your PHP file

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.

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

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.

发布评论

评论列表(0)

  1. 暂无评论