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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:32:16+00:00 2026-06-09T04:32:16+00:00

I have a javascript functions that fetches some JSON from a PHP. When I

  • 0

I have a javascript functions that fetches some JSON from a PHP. When I get the JSON my plan is to parse it and load it in to an array and then return that array so that I can use the data anywhere.

This is the ajax function:

function get_ajax(route_val){

$.ajax({
            url: "ajax.php",
            dataType: 'json',
            data: { 
                route: route_val
        },
            success: function(result) {
                if(result.error == true){
                    alert(result.message);
                }else{

                    $.each(result, function(key1, value1){

                        //console.log(key1 + ":" + value1);

                        returnarray[key1] = value1;

                    });             

                    return returnarray;
                }

            }
        });


}
</script>

If I then try to define say var arr = get_ajax(‘1’), arr is going to be empty. I can alert and console.log stuff from the array from inside the function but returning it returns nothing.

It does not seem to exist outside of the function.

Any ideas?

  • 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-09T04:32:18+00:00Added an answer on June 9, 2026 at 4:32 am

    You are using Ajax incorrectly, the idea is not to have it return anything, but instead hand off the data to something called a callback function, which handles the data.

    IE:

    function handleData( responseData ) {
        // do what you want with the data
        console.log(responseData);
    }
    
    $.ajax({
        url: "hi.php",
        ...
        success: function ( data, status, XHR ) {
            handleData(data);
        }
    });
    

    returning anything in the submit handler will not do anything, you must instead either hand off the data, or do what you want with it directly inside the success function.

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

Sidebar

Related Questions

I've been writing some jQuery functions that have JavaScript variables and looping, etc inside
I have some javascript functions that take about 1 to 3 seconds. (some loops
I'm trying to build some JavaScript in MooTools that fetches table row markup from
I have a PHP script (fetchData.php) that fetches some data and outputs it to
I have an API that is available only in javascript (no PHP). It fetches
I have two JavaScript functions that I want to call from inside of a
I have a javascript that fetches data from mysql and displays it in my
I have a javascript widget which fetches articles from Wikipedia through MediaWiki API (http://en.wikipedia.org/w/index.php?title=Main_Page&action=render).
I have a JavaScript array that contains 150 JavaScript functions (these functions return financial
I have a problem with a javascript set of functions that I made. This

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.