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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:35:46+00:00 2026-05-14T06:35:46+00:00

This is probably really stupid but i can’t find the problem with my code.

  • 0

This is probably really stupid but i can’t find the problem with my code. It fetches a url that returns json and the function is then supposed to return a string:

function getit() {
  var ws_url = 'example.com/test.js';
  var user = false;

  $.getJSON(ws_url, function(data) {
    alert('user '+data.user);//shows john
    user = data.user || false;
  });

  return user;//should return john but returns false
}

test.js will have something like this:

{"id":"12","username":"ses","user":"john","error":""}

or like this:

{"error":"123"}

I also tried if (data.user) {} else {} but it didn’t work either..

So what am i missing?

Thanks 🙂

  • 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-14T06:35:46+00:00Added an answer on May 14, 2026 at 6:35 am

    The problem comes from the fact that the $.getJSON() call is asynchronous. Once the ajax request is fired off, processing continues on inside getit(), and your function immediately returns false. Later on, the Ajax response returns and sets user, but your getit() function returned a long time ago!

    You can force the Ajax call to be synchronous using the async option, like this:

      $.ajax({
        async: false,
        url: ws_url,
        type: "GET",
        success: function(data) {
            alert('user '+data.user);//shows john
            user = data.user || false;
        }
      });
    

    If you do that, your browser will block on the script until the Ajax call returns. Generally this isn’t the ideal behaviour (which is why the default is async: true), so I’d suggest reworking whatever it is that’s calling getit(), and have it expect to process it’s results whenever it can, perhaps by passing it’s own callback function to getit() that would then be executed when getit() is done.

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

Sidebar

Ask A Question

Stats

  • Questions 402k
  • Answers 402k
  • 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 The example you need to study is the following: #nav… May 15, 2026 at 4:48 am
  • Editorial Team
    Editorial Team added an answer Read Numerical Recipes in C, Chapter 3 (especially section 3.6)… May 15, 2026 at 4:48 am
  • Editorial Team
    Editorial Team added an answer Pretty simple if using Group and Having. This should get… May 15, 2026 at 4:48 am

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.