Tips on assigning data values to JavaScript code

How do I assign response data to a variable?

$.ajax({
    type : "POST",
    async: true,
    url : "<?php echo base_url(); ?>" + "Graphs/get_graphs",
    dataType: 'json',
    data :{hotel_name_realm:$("#hotel_names").val()},
    success : function(res){
       obj = JSON.parse(res);
    console.log(obj.upload);
    console.log(obj.download);



series: [{
    name: 'Download',
    data: [
     (obj.Download) // Assign the obj.download data here
    ]

}


}

Take a look at my console.log(obj) image here

Answer №1

I really enjoy doing this,

$.ajax({
      method: "POST",
      url: "chat/searchuser",
      data: {name: $(this).val(), length: "10"}
      })
      .done(function (data) {
      $('#scroll_user').val('10');
      var content = '';
      $.each(JSON.parse(data).data, function (key, value) {
      content += '<div class="row sideBar-body getUserdata userchat" data-username="' + value.fname + '" data-id="<?php echo $key; ?>" onclick="showDiv()" id="' + value.fname + '"><div class="col-sm-3 col-xs-3 sideBar-avatar"><div class="avatar-icon"><img src="<?= base_url('assets/user2.jpg') ?>"></div></div><div class="col-sm-9 col-xs-9 sideBar-main"><div class="row"><div class="col-sm-12 col-xs-12 sideBar-name" data-username="' + value.fname + '"><span class="name-meta">' + value.fname + ' ' + value.lname + '(' + value.username + ')</span></div></div></div></div>'
      $('#usersindiv').html(content);
     });
    }).fail(function () {
     alert("something went wrong");
     });

Answer №2

Keep up the good work, you're on the right track, just don't forget to include the correct square bracket. Remember, JavaScript is case sensitive, so obj.Download is not the same as obj.download.

$.ajax({
    type : "POST",
    async: true,
    url : "<?php echo base_url(); ?>" + "Graphs/get_graphs",
    dataType: 'json',
    data :{hotel_name_realm:$("#hotel_names").val()},
    success : function(res){
       obj = JSON.parse(res);
       console.log(obj.upload);
       console.log(obj.download);

       series: [{
          name: 'Download',
          data: [
            (obj.Download) // Isn't it supposed to be obj.download? (with a small D)
          ]
       ] // Make sure to add the missing closing bracket here
    }
}

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

Can someone explain to me the utilization of async await in conjunction with map in mongoose?

const fetchOwnerInfo = async _ => { console.log('Initiating search for book owners...') const ownerPromises = books.map(async (book) => { const ownerDetails = await User.find({_id: book.Owner}) ...

Converting string patterns to regular expressions

I am utilizing mongodb to store data. My requirement is to save complete regular expressions as strings: { permissions: [{ resName: '/user[1-5]/ig', isRegex: true }] } Although I am aware of the existence of the module mongoose-rege ...

Tracking the frequency of text values that have been clicked on

Feeling uncertain about the best direction to take. In a table of unknown length, the first column consists of clickable links, while the second column contains corresponding text. The number of rows in this table is variable and depends on search resul ...

I am having trouble having a select component populate with new child components while still maintaining its original value

So here's the situation. Let me simplify things for you. I'm in the process of developing an app that generates JSON queries to be sent to a server. The query-building components are structured in a nested manner: QueryContainer > QueryGroup ...

What is the best way to utilize a single Google Map component instance across multiple children?

Seeking a method to maintain the same Google Map instance throughout my entire app, as each map load incurs charges... Currently utilizing google-map-react. An instance of a new Map is created in ComponentDidMount, suggesting that it's important to k ...

The custom directive containing ng-switch isn't being reevaluated when the value is updated

I have developed a unique directive that acts as a reusable form. The form includes an error message display section which utilizes ng-switch: <div ng-switch="status"> <span ng-switch-when="Error" class="text-error">An Error occurred</spa ...

Calculate the total of additional user inputs and show the result in a separate element using JavaScript

Query Is there a way for an element to dynamically show the total of different inputs? Situation I have multiple text boxes all with the same class. There are 4 input fields, each containing a number - Input 1 is "1", Input 2 is "2", and so on. The goal ...

HTML5 Video and jQuery Lazyloading, no controls included

I'm encountering some difficulties with implementing lazyloading for HTML5 Video elements using jQuery. Here is the HTML markup: <section class="video_content" data-poster="imageURL" data-alt="" data-mp4="mp4URL"data-webm="webmlink" data-ogg="ogv ...

Having trouble with my JavaScript onClick function in obtaining the expected output value

Hey there, I've encountered an issue where using a loop to iterate through an array and check the onclick function results in all array elements producing the same result when clicked. The expected behavior is for only the first element's result ...

Verify if the date string includes the day

Here's the scenario: a user clicks on one of two links, either 2012/10, or 2012/10/15. I am extracting the day value from the link to pass it onto an AJAX request that will change days on an archive page. Is using a regular expression like /\d{ ...

I keep receiving a JavaScript alert message saying "undefined."

When I try to alert the item_id after giving input in the quantity textbox, I am receiving an undefined JavaScript alert. The code snippet below is not displaying the alert as expected: var item_id=$("#item_"+i).val();alert(item_id); In addition, my mode ...

Issue with Bootstrap Carousel Interval Setting not Functioning as Expected

I recently added Twitter Bootstrap-Carousel to a website with the intention of using it to navigate through different sections of a module. However, I'm encountering an issue where setting the interval to false does not work as expected. When I set an ...

Prevent body scrolling when modal view is activated

As I work on developing this website, I have encountered an issue. Upon clicking a link located at the far right end of the second row in the header, a modal window appears with embedded YouTube videos. The problem arises when scrolling through the modal: ...

Yeoman refuses to generate a backbone application scaffold

When I try to use yeoman and generator-backbone to scaffold a backbone app, I encounter some issues. Every time I run yo backbone, I receive the following messages: After completion, it tries to run bower install for you in order to install necessary depe ...

Utilizing Vue alongside Laravel by passing null values as props

Is it permissible to provide a null prop? I have created a main component that accepts the user prop. <div id="app"> <master :user="{{ $user }}"></master> </div> The prop is declared as follows: props : { user: { ...

I'm encountering an error after updating Angular/Typescript. The error message states that Type 'Observable<unknown>' cannot be assigned to type 'Observable<ProcResult[]>'

My Angular database service has been set up in this manner since Angular 6, and I am now encountering this error for the first time. I have a standard HTTP POST method that returns a result. In case of an error, I want to handle it appropriately. The most ...

The .val() and focus() methods are not functioning correctly

I am having an issue with a simple script: when I input a link to an image in the form's INPUT field, it should automatically display a preview of the image: https://example.com/image.jpg However, when I input the same link not by using ctrl+C/ctr ...

Load jquery dynamically in Internet Explorer

My goal is to load jquery as a fallback option if CSS transitions are not supported. I'm using modernizer for this purpose, and it's working well. I have noticed that jquery works perfectly when included in the header like this: <script type ...

To insert a new row into a table with an onClick function, along with removing this attribute from all other rows except for the newly added one

I recently created a piece of code that enables the addition of a new row and removes an (onClick) attribute when clicking on an existing row within a table. <table style="vertical-align:middle;margin:20px;" id="table_insert"> <tr id="tra" > ...

Trigger a click event on a nested Angular 13 component to remove a class from its grandparent component

In my Angular 13 project, I am working with 3 components: Child Component : Burger-menu Parent Component : Header Grand-Parent Component : app.component.html Within the burger-menu component, there is a close button. When this button is clicked, I want t ...