Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9153279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:14:57+00:00 2026-06-17T12:14:57+00:00

this is what I have: function loadGraphs(datawijk){ $.ajax({ url: ‘./api5.php’, //the script to call

  • 0

this is what I have:

function loadGraphs(datawijk){

    $.ajax({                                      
      url: './api5.php',                  //the script to call to get data          
      data: {
       wijk: datawijk,
        },                        //you can insert url argumnets here to pass to api.php
                                       //for example "id=5&parent=6"
      dataType: 'json',                //data format      
      success: function(rows)          //on recieve of reply
      {
        var htmlContent = "";
        // ADD TO 

        htmlContent += '<tr><th scope="row">Geboortes</th>';

        $.each(rows, function(i, data) {
          $.each(data, function(j, year) {
            htmlContent += 
              '<td>' + year + '</td>';
          });
        });

        htmlContent += '</tr>';

        var lol = updateOverlijdens(datawijk, htmlContent);
        alert(lol);

        $('#graphs table tbody').html(htmlContent);
      }   
    });
}

function updateOverlijdens(datawijk, htmlContent){

    $.ajax({                                      
      url: './api4.php',                  //the script to call to get data          
      data: {
       wijk: datawijk,
        },                        //you can insert url argumnets here to pass to api.php
                                       //for example "id=5&parent=6"
      dataType: 'json',                //data format      
      success: function(rows)          //on recieve of reply
      {
        // ADD TO 

        htmlContent += '<tr><th scope="row">Overlijdens</th>';

        $.each(rows, function(i, data) {
          $.each(data, function(j, year) {
            htmlContent += 
              '<td>' + year + '</td>';
          });
        });

        htmlContent += '</tr>';

        return htmlContent;
      }   
    });
}

When I do alert(lol); in function loadGraphs I get undefined …
And when I do alert(htmlContent); in function updateOverlijdens just before I return the value I get it right. Only when I alert the value in my function loadGraphs I get undefined. How can I fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:14:58+00:00Added an answer on June 17, 2026 at 12:14 pm

    As explained by a lot of answers, the problem is that you’re trying to return something from the AJAX call back to the function which initiated the call. This cannot work, since the caller does not wait for the AJAX call to complete and will return before it is completed. That’s the whole idea about asynchronous calls: you don’t want to block the execution to wait for the results to come in.

    One way to handle this is by passing a callback function which needs to be executed when the results are retrieved. This function will then be called from within the AJAX success callback.

    Another interesting approach is to make use of jQuery’s deferreds and promises. A promise represents some value which will be retrieved somewhere in the future. A deferred produces a promise and resolves it later on. For example, all AJAX functions return a promise and you can retrieve a promise from any jQuery object which is resolved when all animations are completed.

    In your case, you can create a deferred which is resolved with the htmlContent when the AJAX results are retrieved. You return the promise of that deferred from the function so the caller can bind callbacks to it or combine it with other promises.

    function updateOverlijdens(datawijk) {
        // Create the deferred
        var dfd = new jQuery.Deferred();
        // Initiate the AJAX request
        $.ajax({                                      
            url: './api4.php',
            data: {
                wijk: datawijk,
            },
            dataType: 'json',
            success: function(rows) {
                var htmlContent = '<tr><th scope="row">Overlijdens</th>';
    
                $.each(rows, function(i, data) {
                    $.each(data, function(j, year) {
                        htmlContent += '<td>' + year + '</td>';
                    });
                });
    
                htmlContent += '</tr>';
    
                // Resolve the deferred
                dfd.resolve(htmlContent);
            },
            error: function() {
                // An error occurred, reject the deferred
                dfd.reject();
            }
        });
        // Return a promise
        return dfd.promise();
    }
    

    BEGIN EDIT

    Thanks to Benjamin Gruenbaum for pointing out my usage of the deferred anti-pattern. Here’s an implementation using .then to do the chaining:

    function updateOverlijdens(datawijk) {
        return $.ajax({                                      
            url: './api4.php',
            data: {
                wijk: datawijk,
            },
            dataType: 'json'
        }).then(function(rows) {
            var htmlContent = '<tr><th scope="row">Overlijdens</th>';
    
            $.each(rows, function(i, data) {
                $.each(data, function(j, year) {
                    htmlContent += '<td>' + year + '</td>';
                });
            });
    
            htmlContent += '</tr>';
    
            return htmlContent;
        });
    }
    

    END EDIT

    You can then use the promise in your loadGraphs AJAX success callback like so:

    // Produce the table header
    var htmlContent = '<tr><th scope="row">Geboortes</th>';
    $.each(rows, function(i, data) {
        $.each(data, function(j, year) {
            htmlContent += '<td>' + year + '</td>';
        });
    });
    
    var promise = updateOverlijdens(datawijk);
    promise.then(function(htmlOverlijdens) {
        // Append to the previously created HTML content
        htmlContent += htmlOverlijdens;
        // Apply the HTML
        $('#graphs table tbody').html(htmlContent);
    });
    

    The advantage of using promises is that they give caller much more flexibility. The caller can easily register multiple callbacks using then(), combine it with other promises using jQuery.when() or pipe the results to another promise using pipe() then.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function : $(#inc<?php echo$key; ?>).click(function(){ prod_id = <?php echo$key; ?>; $.ajax({
I have this function that makes an ajax call. I'm describing the problem in
I have this function $.fn.validate.checkValidationName = function(id) { $.post(PHP/submitButtonName.php, {checkValidation: id}, function(data) { if(data.returnValue
I have this function // add history paths and save data function AddPath( strTag,
I have this function: function validate($data) { $newData = str_replace(&nbsp;, , $newData); $newData =
I have this function above to create url slugs from posts title, the problem
I have this function to get the HostAddress from my request (HttpServletRequest) on Java.
I have this function in javascript: $('#myElement').load('/some/url/', { parameter1: 0, parameter2: 1 }); How
I have this JavaScript which should loop through my XML and get my data.
I have this function: function Connect($url, $post = 0, $postfields = '') { $ch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.