What is preventing me from successfully sending data to a .json file using Ajax?

I'm trying to create a basic server that can send data to a .json file and then retrieve that data from another page. However, I am facing an issue with storing the data in the .json file.

I attempted to use the code below, but it was unsuccessful:

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

Answer №1

Merely attempting to push data to a JSON file via POST won't be effective, as the server must actively receive requests and process the payload sent. A viable solution could involve creating a basic script using NodeJS or PHP (or any other suitable server-side language) to manage the storage of the payload in a JSON file.

Answer №2

When sending a POST request, data is transferred to a web server.

The web server has the ability to process the data received in the POST request.

By default, no action is taken. Just imagine the chaos if anyone could send a POST request to another person's server and start creating new files on it. The homepage of Google would be defaced constantly.

If you wish to save the results of a POST request, then you must write server-side code to handle it (and you'll likely want to include authentication and authorization processes as well).


Keep in mind that attempting to directly create JSON with the value of data: in your code example will never result in valid JSON. Avoid manually constructing JSON strings; instead, utilize a library function such as JSON.stringify.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AngularJS: Batch processing for saving multiple students simultaneously

I have been working on saving information about students, and I've written the code below. However, I'm unsure how to proceed from here. Any additional information or resources related to this topic would be greatly appreciated. <div ng-contr ...

Different ways to dynamically change tailwind colors during execution

Utilizing tailwind v3, it's feasible to customize existing colors by modifying the tailwind.config file. https://tailwindcss.com/docs/customizing-colors module.exports = { theme: { extend: { colors: { gray: { ...

Using Select2 JS to populate dropdown options with an AJAX response

I have a large list of pincodes ranging from 20,000 to 25,000. I want the user to search for the first 3 digits of a pincode and then trigger an ajax request to fetch a list of pincodes that match those 3 digits. Although I am successfully receiving a res ...

Leverage async and await features in TypeScript aiming at ES5 compatibility

Currently working on a TypeScript project that is set to target ES5, I am exploring the feasibility of incorporating async/await functionality. Syntactically, the TypeScript compiler is able to transpile the code without issues. However, it has come to my ...

The display of treepanel response text on an extjs grid within a layout-browser is achieved through the use of the

I am attempting to showcase an ExtJS grid and tree within a layout-browser. The tree's response data is displayed in the ExtJS grid, which then dynamically refreshes. My goal is that when I click on a tree leaf, the data from that leaf should be displ ...

Tips for displaying real-time error notifications from server-side validation using AJAX

Seeking a way to display inline error messages from my Symfony2 Backend without refreshing the page. I considered replacing the current form in the HTML with the validated form containing the error messages returned by the backend through AJAX. However, ...

Guide to sending properties to reducer in React with Redux

I've been grappling with the complexities of react-redux for hours. I'm aiming to display an <Alert /> to the user when the value of isVisible is true. However, I'm still struggling to grasp the architecture of redux. Despite my effort ...

Having trouble getting event modifiers to work in Vue when added programmatically

Trying to dynamically add events using v-on through passing an object with event names as keys and handlers as values. It appears that this method does not support or recognize event modifiers such as: v-on=“{‘keydown.enter.prevent’: handler}” T ...

Invoke a TypeScript function from the HTML code embedded within a TypeScript component

In my pop-up window, there are 2 buttons: Update and Delete. I need to implement functionality so that when the Update button is clicked, the current pop-up should disappear and a new editable pop-up with the same fields should appear, along with two addit ...

Whenever trying to access data from the database using DataTable row(), it consistently remains undefined and fails to retrieve any information

While working on my application, I attempted to display all the data in a DataTable from the database. Unfortunately, instead of achieving this, I encountered an error from the server. Upon receiving the error, the dataTable object was displayed as follows ...

Steps to activate the image viewer for each individual looping image:

I'm struggling with my JavaScript code that fetches images and displays them in a modal view when clicked. The issue I'm facing is that the image view only works for the second and other images, but not for the first one. I know there are issues ...

Execute an UPDATE query in PostgreSQL for each item in the array

Imagine a scenario where a cart filled with various grocery items, each having a unique ID, is ready for purchase. When the "purchase" button is clicked, an array containing objects of each item in the cart is sent. The number of items in the cart can vary ...

jquery button returned to its default state

Jquery Form Field Generator |S.No|Name|Button1|Button2| When the first button is clicked, the S.No label and name label should become editable like a textbox. It works perfectly, but if I click the second button, the field becomes editable as expected, ...

Having trouble passing JSON data from JQuery to ASP.Net MVC using AJAX calls?

When I try to call a WCF Service from my ASP.Net web application using jquery to test the service function GetData, I encounter an issue where the parameter value is null. It seems that I am unable to pass the data to the WCF service despite trying various ...

How can you target a specific data-target button while working with multiple Bootstrap collapse elements?

One challenge I'm facing is with the Bootstrap collapse elements on my website. I have several of them, each controlled by a read-more button that includes an icon to indicate the state of the collapse element. However, when I add a class to the butto ...

Setting the current date in the CalendarExtender control is a simple task that can

I have a simple requirement that I need help with. Can anyone provide instructions on how to set the current date in a CalendarExtender control? <cal:CalendarExtender ID="calDate" runat="server" SelectedDate="2008-01-01" TargetControlID="txtDate" CssC ...

"Using jQuery's animate() function to dynamically change text content

I'm currently venturing into the world of jQuery animate() and decided to create a presentation-style project for practice. However, I've hit a roadblock when attempting to alter the text within my div element in the midst of an animation utilizi ...

Jarring Json conversion: Assign a value based on a reference identifier (not an index)

In my NIFI Jolt transformation, I am attempting to link a value from one JSON collection to another based on a key from the original collection (specifically assigning prefixes from EventCodePrefixes using referenced EventCodePrefixId in EventCodes): Samp ...

Is it a suboptimal method to repeatedly use order.find, collect data, and transmit a substantial data payload to the front end in

Currently, I am working with Express, React, and MongoDB but I am relatively new to using express and REST API. My goal is to add an analytics feature to my frontend that focuses on sales and orders. The plan is to allow users to search within a specified ...

Keeping an eye out for modifications within the ng-repeat when updating mongoose from a separate controller

Recently, I encountered a very specific issue that I need help with resolving. Here's the scenario: I have a department controller that loads a collection from MongoDB and displays it in a table using ng-repeat. Each document in the collection has an ...