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 7278753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:55:04+00:00 2026-05-28T22:55:04+00:00

I have an ajax call in jquery which returns 4 different arrays. 3 of

  • 0

I have an ajax call in jquery which returns 4 different arrays.
3 of these arrays i want to use outside of the ajax success function and the data in the arrays should be accessible for certain click events.

I just don’t know the best way to handle the array variables.
some people say going global is a bad idea and other say its ok.. so

ajax success function:
does stuff with one of the arrays

array1, array2 needs to be used by other functions and click events

function display_results_1(){
$('#myDiv').html(array1.id);
}


$('#binfo').click(function(){
    $('#client_info_div').dialog({

    $('#myDiv').html(array2.id);

    });
})

This is my ajax call:

  $('#c_search').submit(function(){
           data = ($(this).serialize());

          $.ajax({
              url: 'actions/get_company.php',
              type: 'POST',
              data: data,
              cache: false,
              dataType: 'json',
              success: function(selected){
`doing stuff here`
    })
    })

Should I use separate Ajax calls to get the data when needed for each different function?

  • 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-28T22:55:04+00:00Added an answer on May 28, 2026 at 10:55 pm

    You definitely don’t want your variables in the global scope, but you could wrap everything in a function to prevent them from being in the global scope:

    $(function() {
        var array1 = [];
        var array2 = [];
    
        function display_results_1(){
    $('#myDiv').html(array1.id);
    }
    
    
    $('#binfo').click(function(){
        $('#client_info_div').dialog({
    
        $('#myDiv').html(array2.id);
    
        });
    })
    
    $('#c_search').submit(function(){
               data = ($(this).serialize());
    
              $.ajax({
                  url: 'actions/get_company.php',
                  type: 'POST',
                  data: data,
                  cache: false,
                  dataType: 'json',
                  success: function(selected){
    `doing stuff here`
        })
        })
    
    });
    

    By doing this you’ll ensure that the code is also running on page load, notice the $() wrapping the function.

    See http://api.jquery.com/ready/ for more info.

    Contrary to the previous answers, going global is NOT a good idea. You may end up having multiple pieces of JavaScript using the same variables.

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

Sidebar

Related Questions

I have a jquery json ajax call and inside my success function I have
On my main page I have this jquery code which does an ajax call
I have a jQuery AJAX call with type:'GET' like this: $.ajax({type:'GET',url:'/createUser',data:userId=12345&userName=test, success:function(data){ alert('successful'); }
A situation I ran across this week: we have a jQuery Ajax call that
i have an ajax call $.ajax({ url: '<%=Url.Action(SaveDetails,Survey) %>', dataType: 'JSON', cache: false, data:
I have an ajax call that fires multiple times but is called once. function
I have an ajax call. I put the return value (data) in a variable
I have an AJAX controller action which returns JSON. The returned JSON is handled
I'm currently using jquery ajax to call a pagemethod (which works great); $.ajax({ type:
I have a WCF Service which returns a string. I am trying to call

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.