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

  • Home
  • SEARCH
  • 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 322491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:56:38+00:00 2026-05-12T08:56:38+00:00

When the form is submitted, I’m calling a function getPosts and passing through a

  • 0

When the form is submitted, I’m calling a function getPosts and passing through a variable str. What I’d like to do is get the data returned from that function.

 // when the form is submitted
 $('form#getSome').submit(function(){
  var str = $("form#getSome").serialize();
  var something = getPosts(str);

  * This is where I'd like to get the data returned from getPosts()

  return false;
 });

 // get the data
    function getPosts(str){

        $.getJSON('http://myurl.com/json?'+str+'&callback=?',
          function(data) {

             arrPosts = new Array();

              $.each(data.posts, function(i,posts){

                // build array here

              });

              return arrPosts;

          });
    };

I’ve tried many things, but have only gotten ‘undefined’ returned. I’ve tried console.log(something);, console.log(getPosts).

I’m missing something very fundamental here. Any help would be greatly appreciated.

EDIT:

What I’m trying to do is create a single function that would get posts. Then different events would call that function. I could then use that data. So one event may be submitting a form, another may be clicking a link, another lazy/endless scrolling. All could use the same getPosts function.

There’s a lot of parsing out the results which amounts to a lot of lines of code. Was just trying to find a way to reuse that function. Do you think that would be possible?

$('a.thisLink').click(function(){
    getPosts();
    get the return from getPosts() and do something with it
});

$('form.thisForm').submit(function(){
    getPosts();
    get the return from getPosts() and do something with it
});

function getPosts(){
   get the posts and return an array
}
  • 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-05-12T08:56:38+00:00Added an answer on May 12, 2026 at 8:56 am

    Ajax requests are executed asynchronously, the callback function (function (data)) of getJSON is executed when the request ends, and returning a value in that callback has no effect, because is a nested function inside getPosts and its return value is never used.

    Actually in your example, getPosts doesn’t return anything and it ends its execution before the data is returned.

    I would recommend you to work on your submit event handler, if you want to keep the getPosts function, you can introduce a callback parameter:

    $('form#getSome').submit(function(){
      var str = $("form#getSome").serialize();
    
      getPosts(str, function (data) {
        var array = [];
        $.each(data.posts, function(i,posts){
          // build array here
          array.push(/* value to add */);
        });
        // array ready to work with...
        //...
      });
      return false;
    });
    
    function getPosts(str, callback){
      $.getJSON('http://myurl.com/json?'+str+'&callback=?', callback);
    }
    

    Edit 2: In response to your second comment, you could make another callback, that will be executed when the data has been processed by the first callback, and you can define it when you execute the getPosts function on the submit event handler:

    $('form#getSome').submit(function(){
      var str = $("form#getSome").serialize();
    
      getPosts(str, reusableCallback, function (result) {
        // result contains the returned value of 'reusableCallback' <---
      });
      return false;
    });
    
    function reusableCallback(data) {
      var array = [];
      $.each(data.posts, function(i,posts){
        array.push(/* value to add */);
      });
      //...
      return array;
    }
    
    function getPosts(str, callback, finishCallback){
      $.getJSON('http://myurl.com/json?'+str+'&callback=?', function (data) {
        finishCallback(callback(data)); // pass the returned value
                                        // of callback, to 'finishCallback' which is
                                        // anonymously defined on the submit handler
      });
    }
    

    Edit 3: I think that the getPosts function and the “reusableCallback” function are strongly related, you might want to join them, and make the code easier to use and understand:

    $('form#getSome').submit(function(){
      var str = $("form#getSome").serialize();
    
      getPosts(str, function (result) {
        // result contains the processed results
      });
    
      return false;
    });
    
    
    function getPosts(str, finishCallback){
      $.getJSON('http://myurl.com/json?'+str+'&callback=?', function (data) {
        // process the results here
        var array = [];
        $.each(data.posts, function(i,posts){
          array.push(/* value to add */);
        });
        //...
        finishCallback(array); // when the array is ready, execute the callback 
      });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 174k
  • Answers 174k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A capture group when searching/replacing with regex in VS can… May 12, 2026 at 2:56 pm
  • Editorial Team
    Editorial Team added an answer I had a bug in an SPI driver that ended… May 12, 2026 at 2:56 pm
  • Editorial Team
    Editorial Team added an answer There are quite a few listed on Functional Programming in… May 12, 2026 at 2:56 pm

Related Questions

When the form is submitted, I'm calling a function getPosts and passing through a
I have an input field which when the enter key is pressed (as there
I currently have my code set to disable the submit button when the field
User is a model. I have 100 users. I need to present the list
I'm making a simple form to create polls, therefore I want the possibility to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.