javascriptjsonajax

Why can't I send data to .json format file by ajax


I'm making a simple server to send data to a .json file and receive that data from another page but I have problem how to store data in .json file.

I used following code but it didn't work:

<script src="jquery/jquery-3.4.1.min.js"></script>

<script>

     var _lname = "x";
    var _fname = "y";
    var _mname = "x";
      $.ajax({
                type: "POST",
                url: "data.json",
                data: "{'lastName':'" + _lname + "','firstName':'" + _fname + "','middleName':'" + _mname + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function () {
                    }
                });

</script>

Solution

  • Simply POSTing data to a JSON file won't work, as sending a POST request requires the server to listen for requests and do something with the payload you sent. You could make a simple NodeJS or PHP script (or any other server-side language for that matter) that could handle saving the payload to a JSON-file.