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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:09:44+00:00 2026-06-02T20:09:44+00:00

I am using the below json call in my javascript method function go123(){ var

  • 0

I am using the below json call in my javascript method

function go123(){
    var cityName = "";
    var temp = $.getJSON("https://abc.in/api/city?callback=?", args,function (data) {
        if (data.properties.city != null){ 
            cityName = data.properties.city;
            check = true;
        } else {
            cityName = "NaN"
        }
    }); // end of my Json Call.

    // my validation is done below
    if(cityName != "NaN"){
        return false;
    } else {
    // here I except the cityName to not be "" but be some value which  is set as :cityName = data.properties.city;
        return true;
    }
} // end of my function 

Now what problem I am facing is that before my Json call is compelete the next set of statements ( in the code below the line “// my validation is done below ” ) is already executed.

I want to get the values set in my json call (cityName) and only once when the call is completed then only I want the next set of statements to be executed.

Please help me on this. Any advice/ideas/suggestions will be highly appreciated ! 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-06-02T20:09:48+00:00Added an answer on June 2, 2026 at 8:09 pm

    AJAX calls are asyncrhonous. They don’t wait for the reply. They operate in the background and execute the code that follows it immediately after the call. Therefore, the data received in getJSON is not yet there when the operations below it are executed.

    You can put the operations you want in the callback so that they get executed when the data is revceived:

    function go123(callback){
        var temp = $.getJSON("https://abc.in/api/city?callback=?", args,function (data) {
            //execute the callback, passing it the data
            callback(data);
        });
    }
    
    //when you call go123, it get's back the result:
    function goBefore123(){
    
        //get our JSON
        go123(function(data){
    
            //when we get our data, evaluate
            if (data.properties.city != null){
    
                cityName = data.properties.city;
                check = true;
    
                alert('executed after returned');
                afterCall();
            } else {
                cityName = "NaN"
            }
        });
    
        alert('i am executed before anything else');
    }
    
    function afterCall(){
        alert('im also executed after');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JavaScript function that I use to call the Facebook API and
Below is a method I'm using to make calls to a REST API. It
using the below code for decoding json $categories = json_decode($data); $categories = $categories->data; where
I'm using the code below for Android JSON parsing but I am getting the
I am using JQuery & JSON (POST) to call webmethod. However I can call
I am using the below method between the 50lines of JQuery Code. From the
I have an asp.net application with a static page method. I'm using the below
I have an ExtJs combobox. Its store loaded using JSON (using MyStore class below).
I'm making an ajax call using jQuery as below. $.ajax({ type: POST, url: proc.php,
Hi all i am using json to pass data in wcf service. Below is

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.