I am having trouble retrieving the array value from the response sent by my server

After receiving a dictionary from my server, when I try to access the values using the following code:

{"filters":{
  "Facture": [
    "Магма (Тычок)",
    "Тонкий кирпич",
    "Гладкий",
    "Крафт",
    "Магма"
  ],
  "Color": [
    "Беж",
    "Черный",
    "Амстердам",
    "Лондон Брик",
    "Мюнхен",
    "Сити Брик"
  ],
  "Name": [
    "Облицованный кирпич стандартный пустотелый",
    "Тонкий колотый с ф. 180/11"
  ]
}}

The console returns undefined. Despite trying various approaches like changing the data sent by the server or modifying the code for accessing the values, I couldn't figure out a solution.

Interestingly, creating the object within the script works correctly, but utilizing the object received in the response fails.

Answer №1

Update this line:

const myObj = JSON.parse(response);

to

const myObj = JSON.parse(response).filters;

as it appears that the desired values are stored within the filters property.

Revised

Interestingly, upon closer inspection, it seems that the server actually returned ""{...}"" instead of just {...}, hence we had to double parse it:

const myObj = JSON.parse(JSON.parse(response)).filters;

If possible, a more ideal solution would be to address this issue on the server or backend side.

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

Decode JSON data retrieved from a remote URL address

I am in need of assistance decoding this JSON code from a URL page. The JSON code on the URL is in the following format: {"current":{"artists_id":"55","albums_id":null,"albums_tracks_id":null},"html_current":"<li><p>Pr\u00e1v\u011b h ...

How to use Gson to deserialize JSON in Java

Struggling with deserializing json in java using gson. This is the json I am working with: {"races":[ {"id":1,"mask":1,"side":"alliance","name":"Human"}, {"id":2,"mask":2,"side":"horde","name":"Orc"}, {"id":3,"mask":4,"side":"alliance","name":"Dwarf"}]} ...

Ways to modify the default text in a dropdown menu?

I'm currently attempting to modify the title of a dropdown using the Multi-select plugin found here. However, I've encountered an issue where I am unable to dynamically change the text (Dropdown title) of the dropdown using JavaScript. $(' ...

Utilizing jQuery.ajax to Send an Array of Objects to a PHP Function

In this scenario, an array of objects is represented as follows: rectangle[0].width = w; rectangle[0].height = h; rectangle[1].width = w; rectangle[2].height = h; rectangle[3].width = w; rectangle[3].height = h; ... We need to figure out how to send thi ...

How to handle a null response in jQuery Ajax when working with JSON data

Querying MySQL data with jQuery Ajax and JSON has been a seamless process for me. However, I am facing an issue where I need to determine if the response is empty or not based on the date parameter passed to my PHP script. Here's a simplified version ...

What are the different methods for completing a form?

From what I understand, there are 2 methods for submitting a form. For instance, in asp.net, there is the Button.UseSubmitBehavior property which Specifies whether the Button control uses the client browser's submit mechanism or the ASP.NET postb ...

Use ag-Grid to customize your column headers with checkboxes, allowing you to easily select or deselect all items in that column. This feature is not limited to

In my experience with ag-grid, I often find myself needing to customize the first column header to include a checkbox. This allows me to easily perform actions such as selecting all or deselecting all rows in the grid. It's important to note that this ...

Incorporate data into the input value rather than the div using Ajax technology

This ajax function is working perfectly, but I would like to modify the location where the result should appear. <script type="text/javascript"> function submitForm1() { var form1 = document.myform1; var dataString1 = $(form1).serialize(); $.ajax ...

Creating multiple Google markers based on addresses retrieved from a database using PHP

I have a requirement to display multiple Google markers on a map based on addresses stored in a database. I have already successfully loaded an XML file to retrieve latitude and longitude coordinates, but now I need assistance on how to create multiple m ...

`Month filter functionality optimized`

I have a flirty plugin installed on my website and it's working great except for one thing: I'd like the month category to be filtered in chronological order instead of alphabetically. Is there a way to achieve this? In simpler terms, how can I ...

When using Loopj to perform a Put or Post request with basic authentication, the response may sometimes be null

In an effort to utilize Loopj for synchronous put and post calls from an HTTP utility class, the code utilizes a synchronous client within an AsyncTask. Certain UI interactions heavily rely on the JSON response, hence the AsyncTask is responsible for man ...

The Vue3 counterpart of vNode.computedInstance

I'm currently in the process of upgrading my app from Vue2 to Vue3, but I've hit a roadblock when it comes to dealing with forms. For my form elements, such as FormInput, FormTextArea, and FormCheckbox, I have been using Single File Components ( ...

Utilize a for loop within a query to showcase a modal popup

I have a specific requirement: I want to display a modal popup window based on a for loop using jQuery. I have attempted the following approach, where I want the modal popup to be displayed based on the value of a flag. For example, if the Flag value is 3, ...

present information through a table utilizing Ajax technology

The following Ajax code is used to display data from Python stored in MongoDB. <script> function displayData(){ $(document).ready(function(){ $.get('ajax1', function(result){ $.each(result, function(index, element) { ...

The reasons behind the static nature of Ajax pagemethods

What is the reason for Ajax pagemethods being static? ...

An issue has been detected by Zone.js where the ZoneAwarePromise `(window|global).Promise` has been unexpectedly replaced

I have recently integrated the Angular2 quickstart code into my existing webpack setup, but I seem to be facing an issue where something is interfering with the promise from zone.js, resulting in an error. Based on my research on Stack Overflow, it appears ...

The Vue v-for directive encountered an unrecognized property during rendering

Trying to grasp the concept of v-for in Vue JS, especially since I am a newcomer to this framework. Since I am utilizing Django, custom delimiters are necessary. I have a script example that appends a list of objects to a data property: var app = new Vue( ...

Running JSON in Selenium framework

Can someone help me with the simplest method to implement JSON in a Selenium test? For instance, I am looking to send a POST request with { "UserId":"234234" } and verify the response. Right now, I'm manually testing using the Advanced REST client Chr ...

When a directive is passed a string with HTML tags, the tags are not displayed/rendered as expected

When it comes to passing a string with HTML content to my Angular directive, I've encountered an issue where the rendered output treats the HTML as text. While researching possible solutions, most of them seem to rely on using ng-bind-html directive, ...

Error message: "Serialization of object to ajax/json cannot be completed

I have encountered an issue while attempting to pass a List MyModel via AJAX/JSON to the controller. I noticed that all the objects are being passed as 'undefined': [Form Post Data] undefined=&undefined=&undefined=&undefined=&un ...