Manipulating and storing a JSON array using ajax

I'm struggling with updating my json array properly. Even though I am able to remove an object from the array, when I check the server's json array, it still appears unchanged. What could be causing this issue?

Below is the function I am currently using:

$(function() {
    $("#rectangle-delete").click(function() {

      var selection = $('#templateSelection > option:selected').text();

      var json = (function () {
        var json = null;
        $.ajax({
            'async': false,
            'global': false,
            'type': 'POST',
            'contentType':"application/json",
            'url': 'server/php/data/' + selection,
            'dataType': "json",
            'success': function (data) {
                json = data;
            }
        });
        return json;
      })();

      var ID_clicked = $(".rectangle.selected.targeted").attr('id');

      console.log('initial array is ' + json);

      json.some(function(e) {
        if (e.ID === ID_clicked) {

            var values = json.map(function(e) { return e.ID; });
            var index = json.map(function(e) { return e.ID; }).indexOf(ID_clicked);
            var data = JSON.stringify(json[index]);

            json.splice(index, 1);

            return true; // stop the array loop
        }
      });

      console.log('new array is ' + json);
    });
});

In the console, I can see:

initial array is [object Object],[object Object],[object Object]

and then

new array is [object Object],[object Object]

However, despite these changes, the actual json file on the server remains unaffected.

Answer №1

Retrieving the JSON data from the server does not provide a direct link to the actual object on the server; instead, it gives you a duplicate of the information.

As a result, any modifications made are solely affecting the client-side version of the data.

If there is a need to update the server's object, it is important to inform the server of the changes (or reconsider the approach by having the server handle the computation and send updated results back to the client).

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

Capture the HTML code from the current page and store it in a variable using the post method

My goal is to capture and pass the current HTML code of a page using the method POST. I have implemented the following code: var html_document = document.documentElement.innerHTML; document.body.innerHTML += '<form id="myform" action="myurl.php" ...

Unable to reach socket.io.js on Raspberry Pi using Lighttpd [Node.JS & Socket.IO]

I just started learning about Node.JS and Socket.IO yesterday. I've been attempting to set up Node.JS and Socket.IO on my Raspberry Pi, but I can't seem to get it working. I'm unable to access <myip>:1337/socket.io/socket.io.js. I fol ...

What is the best way to integrate the express-session logic with Prisma for optimal performance?

Hi there, I recently started using Prisma and want to integrate it with PostgreSQL. My main goal is to implement authentication in my backend, but I encountered issues while trying to create a session table. When working with raw SQL, I managed to add the ...

Accessing non-array elements using array-style syntax (such as true, false, null, etc)

In my array structure, it looks like this: $this->response = [ 'code' => null, 'errors' => null, 'data' => null, ]; Currently, when I check for errors, I do it like this: if ($response['errors&a ...

What is the best way to conceal elements that do not have any subsequent elements with a specific class?

Here is the HTML code I have, and I am looking to use jQuery to hide all lsHeader elements that do not have any subsequent elements with the class 'contact'. <div id="B" class="lsHeader">B</div> <div id="contact_1" class="contac ...

The image is failing to animate according to the PNG sequence function

Enhanced Functionality: Upon clicking the "Tap Here" image button, a function called "GameStart()" is triggered. This function ensures that the main image "Star" descends down the page from the top through an animated sequence using png logic. The propose ...

Tips for deleting key value pairs from a JSON file using Java

Can anyone provide me with guidance on how to manipulate the provided dummy JSON file using Java? The head object in the file contains various values and children that follow a similar structure. I am looking for a way to eliminate all keys where the val ...

Only the first result is printed by mysql_fetch_array

Similar Query: Looping through mysql_fetch_array in PHP I am facing an issue with my join query where I try to print out multiple array results using a foreach loop. Despite having 801 results in the array, only the first result gets printed. If anyon ...

Assistance in managing a substantial volume of information

I currently have an array containing 500 objects, but I've been advised against it due to the high memory consumption. I was recommended to use Core Data SQLite instead to efficiently load just one object at a time. However, I'm unsure if Core Da ...

The issue of jQuery file upload not functioning on every row of an ASP.NET repeater

Incorporating a jQuery file upload button in each repeater row has resulted in a display issue where only the selected files for the first row are shown. For subsequent rows, only the total count of selected files is displayed without the progress bar sh ...

I am looking to pass the value of the textbox to the controller in the MVC framework

In my view, I have a textbox set up like this: < input type="text" id="Quant" value="@item.Quantity"/> My goal is to pass the value of this textbox to an action method when it is changed. Here is my action method: public ActionResult Quant(int id ...

Can you transform a character array into an array of objects in Java? Is this doable?

public static String censorInput(String inputString) { Object[] charArray; int length = (charArray = inputString.toCharArray()).length; //THIS LINE OF CODE IS CAUSING A PROBLEM The purpose of converting it to an object is to be able to cast ...

What is the optimal method for verifying two distinct conditions simultaneously using Javascript?

Hey there, I'm working on a code snippet to check the status of a Rails model. Here's what I have so far: var intervalCall = setInterval(function(){ $.post("getstatus", {id:id}); var finished = "<%= @sentence.finished%>"; // CONDI ...

Looking to empty a textbox, give it focus, and avoid triggering an ASP.NET postback all with a single click of a

I am working on an ASP.NET project and I have a simple HTML button. When this button is clicked, my goal is to clear textbox1, set the focus on textbox1, and prevent any postback from occurring. However, I am running into an issue where preventing postba ...

When making an ajax call, I passed the data "itemShape" but on the URL page, an error appeared stating "Undefined index: itemShape"

Hello, I have been using an Ajax function to send data with the key itemShape. However, when I directly access the URL page or service page, it displays the following error: Notice: Undefined index: itemShape in C:\wamp64\www\inventory_so ...

Exploring Grails Assets, Redirections, Secure Sockets Layer, and Chrome

Using Grails 2.1.1 with the resources plugin, I have encountered an issue when incorporating the jstree library which comes with themes configuration: "themes":{ "theme":"default", "dots":false, "icons":true } The JavaScript in the library locat ...

Attempting to authenticate a token within a Node.js environment

Once a user completes the sign-up process, I send them an email containing a unique token and their email address. When they click on the link provided in the email to verify their account, I attempt to authenticate the token by extracting the token object ...

broadcast a video file from a Node.js server to multiple HTML5 clients at the same time

After researching online, I have been looking for a way to simultaneously stream a video.mp4 to multiple html5 clients. Despite reading various tutorials, I haven't found the ideal solution using nodejs. Do you have any suggestions or alternative met ...

What is the best way to retrieve users.cache from all shards and consolidate them into a collection similar to the one returned by client.user.cache?

My discord bot originally used the following code: client.users.cache This would provide me with a Collection[Map] containing all the cached users. Recently, I implemented sharding and attempted to replicate this using: client.shard.fetchClientValues(&ap ...

What is the reasoning behind having two separate permission dialog boxes for accessing the webcam and microphone in flash?

I am currently using a JavaScript plugin known as cameratag () in order to record videos through the web browser. This plugin utilizes a flash-based solution. When the flash application requests permission to access the webcam, it presents a security dialo ...