accessing an array beyond the scope of an AJAX request

I'm struggling to use the data from an array called struct, which I am fetching through ajax. Here is my code:

var raw_data = null;
$.ajax({
    url:path.url_respuesta_leer, async:false,
    type:"post", dataType:"json", data:{form:id},
    success : function(obj) {
        var raw_data = obj.struct;
    }
    //console.log(raw_data) show: [Object, Object, Object] 0:Object label:"Some text"
});

var new_data = [ {"Title": raw_data[0].label } , etc...

The console keeps showing that it's undefined. I know it should be simple, but for some reason I can't figure it out. Any help would be appreciated.

Answer №1

Avoid redefining the same variable within the ajax success block where you are storing the data.

var raw_data = null; // ensure this is only declared once
$.ajax({
    url:path.url_respuesta_leer, async:false,
    type:"post", dataType:"json", data:{form:id},
    success : function(obj) {
        //var raw_data = obj.struct; avoid using 'var'
        raw_data = obj.struct;
    }
    //console.log(raw_data) show: [Object, Object, Object] 0:Object label:"Some text"
});

var new_data = [ {"Title": raw_data[0].label }

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

Generate a hyperlink within a paragraph

Can anyone provide tips on how to turn a string from Json into a paragraph with a hyperlink included? <p>Dumy Dumy Dumy Dumy Dumy Dumy Dumy DumyDumyDumyDumy abc.com </p> Currently, the paragraph displays as is, but I would like to make abc.c ...

Wildcard whitelisting in middleware systems

Currently, my app has a global middleware that is functioning properly. However, I am now looking to whitelist certain URLs from this middleware. The URLs that I need to whitelist have the format api/invitation/RANDOMSTRING. To achieve this, I considered ...

Guide to resetting a CSS animation with pure JavaScript

My flexbox contains 4 images, each triggering an animation on hover. However, the animation stops if I hover over the same image again. I have tried various JavaScript solutions found online to restart the animation, but none seem to be working for me. S ...

modify column data using a JSON object

When attempting to update a field with JSON data in the TEST_TABLE: UPDATE TEST_TABLE SET JSON_DATA = "{ "Business_Type": "载货", "Collected_Article_Quantity": null, "Consignee_Company_ContactPerson": n ...

What is the best way to retrieve my files stored on my express server?

I have successfully uploaded my images to my server using Multer, but now I am struggling to serve that file. When I try to access the path: http://localhost:3001/public/backend/public/uploads/user-admin-1556247519876.PNG, I receive a 404 error. I believe ...

Using PHP to perform a JSON query for fetching geolocations from db-ip.com and saving the results into distinct variables

Attempting to utilize db-ip.com for IP geolocation resolution. Acquired an API key and the correct request URL: JSON response: {"address":"11.11.11.11","country":"US","stateprov":"Texas","city":"Houston"} PHP script executed through debian linux shell ...

I am encountering issues with my PostCSS plugin not functioning properly within a Vue-cli 3 project

I developed a custom postcss plugin that was working perfectly according to the postcss guidelines until I tried to implement it in a real project. For reference, here's the plugin on GitHub My goal is to integrate it into a Vue-cli app using Webpac ...

Manipulating values within a multidimensional array using JavaScript

I am attempting to populate a multidimensional array with data from a JSON file. The issue I am facing is that I am unable to update the content in the array: if(j>i && hoursYy == hoursY && secondsX == secondsXx){ wocheArray[i].count = wocheArray[i] ...

When the Popover appears in ShadCN, the input field unexpectedly takes focus

This unique component incorporates both Popover and Input functionality from shadcn. An issue I have encountered is that when I click on the Input to trigger the Popover, the Input loses focus and the cursor moves away from it. Expected outcome: Upon c ...

Combining JSON arrays into rows in PostgreSQL using Node.js

If I have an array of JSON like this: [ {"a":1,"b":2},{"a":3,"b":4} ,{"a":5,"b":6} ] How do I properly insert this data into a PostgreSQL table like so: in out 1 2 3 4 5 6 I've researched JSON datatypes in PostgreSQL, but I'm struggling t ...

Methods for organizing the output of a saved routine and sending it back to JavaScript

if (gameName.Contains("Game1")) { using (var conn = new SqlConnection(SessionWrapper.BackOfficeSecondaryDbConnectionString)) { var query = @"crm_Game_category"; var param = new DynamicParameters(); pa ...

Enhancing jQuery Mobile listview with voting buttons on each item row

I am looking to incorporate 2 vote buttons within a jQuery mobile listview, positioned on the left-hand side and centered within each list item. While I have managed to achieve this using javascript, my goal is to accomplish it without any additional scrip ...

My search bar in the navbar is sticky, but unfortunately it still ends up covering the page content as you scroll down

I am trying to figure out how to prevent the search bar (not the top navigation bar) from overlapping with the page contents when scrolling down. Just to clarify, I have both a navigation bar and a search bar, but the issue I'm addressing specifically ...

Differences between Angular 4 and AngularJs directives

Delving into the world of Angular 4, I have encountered a slight hurdle in my understanding of directives. My goal is to create a directive that can resize an element based on its width. Back in the days of AngularJs, this task was accomplished with code r ...

What is the best way to retrieve serialized JSON from a Flask-SQLAlchemy relationship query?

I am currently utilizing Flask-SQLAlchemy and have the following models that are related in a one to many relationship: class User(db.Model): # Table name __tablename__ = "users" # Primary key user_id = db.Column(db.Integer, primary_key= ...

Steps for converting the current JSON_OBJECT into a list format

Struggling to grasp how I can swap results in my query. This code interacts with an Oracle database: TO_NCLOB(JSON_OBJECT( 'name' VALUE TRIM(tb1.DS_NAME), 'birth' VALUE tb2.DT_BIRTH, 'id' V ...

Leverage React.js by incorporating a Header-Imported JavaScript Library

I recently developed a React.js web application and am exploring the use of Amplitude Analytics, which provides a Javascript SDK found here. According to the guidelines, I need to include a <script></script> within the <head></head> ...

Processing and displaying JSON data along with nested attributes

Here are some of the classes I have: class Product attr_accessible :name, ... has_many :images end class Image attr_accessible :image, :position, :product_id, :title belongs_to :product In this case, I am trying to achieve the following action: def ...

Retrieving a value attribute from the isolated controller of a directive

I have a directive that reads and writes attributes, but I'm having trouble getting it to work as expected. The issue seems to be with the controller inside main-directive.js, which is empty, while the actual action is happening in the isolated direct ...

Tips for updating an Angular renderer's value

I have a challenge with implementing a background color change when clicking on a th element. Currently, it works fine when I click on it, but I would like to make it so that if I click on another th element, the previous one will turn off. However, if I ...