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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:41:23+00:00 2026-05-18T06:41:23+00:00

I made a script to get JSON data from a file on our server

  • 0

I made a script to get JSON data from a file on our server using AJAX but I’m having trouble putting it into a function.

Here’s my code:

function getJSON (file)
    {
      var request = AjaxRequest();
      var json = "";
      request.onreadystatechange = function ()
      {
         if(request.readyState==4 && request.status==200)
            {
              json = JSON.parse(request.responseText);
            }
      }
      request.open("GET", file, false);
      request.send();
      return json;
     }

The function does everything I want it to but I’ve been told to NEVER pass false to the AJAX request because of blocking. Something just seems wrong about this function but I have no idea how to change it. Should I change it? If so, how?

  • 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-18T06:41:24+00:00Added an answer on May 18, 2026 at 6:41 am

    You can’t return it like this, it’s an asynchronous operation, meaning your json = JSON.parse(request.responseText) happens later, when the server responds with data…long after you returned.. Instead you can pass in a function which accepts the data, like this:

    function getJSON (file, callback)
    {
      var request = AjaxRequest();
      request.onreadystatechange = function ()
      {
         if(request.readyState==4 && request.status==200)
         {
           callback(JSON.parse(request.responseText));
         }
      }
      request.open("GET", file, false);
      request.send();
    }
    

    Then you call it like this:

    getJSON("something.json", function(data) { 
      //use data, your json object/data
    });
    

    This way you’re using the data when it’s available, passing it onto the next function…this is the way asynchronous calls are intended to behave.

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

Sidebar

Related Questions

I have made a chat script using php, mysql and jquery. It uses json
I'm trying to load a jqGrid from json data made via a REST call.
I have a jQuery ajax function that retrieves JSON data. In the success block
hi i'm using a script made by http://snook.ca/archives/javascript/simplest-jquery-slideshow so basically the code i have
i had made a small bash script in order to get the frequency of
I've made jQuery & AJAX script that uses a PHP function to bring back
Made solution change: I am trying to display a html table of data.. In
I made a class from Linq to SQL Clasees with VS 2008 SP1 Framework
I made a class that derives from Component: public class MyComponent: System.ComponentModel.Component { }
I made a code that translate strings to match each word from the array

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.