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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:57:19+00:00 2026-06-04T15:57:19+00:00

I am trying to return a JSON object from a method (pollServiceForInfo), but it

  • 0

I am trying to return a JSON object from a method (pollServiceForInfo), but it seems to get “lost” when I alert it once the method has finished. I know this is a scoping issue, however I am stumped as how to proceed. Insight would be greatly appreciated.

var id=null;
var jsonData = JSON.stringify( {searchRequest:{coordinates: "1,2,3 1,2,3 1,2,3 1,2,3 1,2,3"}} );
$.post("rest/search",jsonData, function(json){
    id = json.searchResponse.id;
})
.error(function(jqXHR, textStatus, errorThrown){
    alert("obj.responseText: "+jqXHR.responseText + "  textStatus: "+textStatus+"  errorThrown: "+errorThrown);
})
.success(function(data, status, obj){
    // process initial request
    var json = pollServiceForInfo(id);  // method below

    alert(json);  // says undefined
});



var pollServiceForInfo = function(id){
    //alert('id in pollServiceForInfo '+id);    
    var jsonResults;
    $.get("rest/poll/"+id,function(data){
        jsonResults = data.pollResponse;

    }).error(function(){ 
        alert('returning error');
        return "error";
    }).success(function(){
        alert('returning data '+jsonResults);
        return jsonResults;  // is lost after it's returned
    });
};
  • 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-04T15:57:21+00:00Added an answer on June 4, 2026 at 3:57 pm

    You can’t usefully return from an asynchronous function. Do this instead:

    var pollServiceForInfo = function(id, callback){
        //alert('id in pollServiceForInfo '+id);    
        var jsonResults;
        $.get("rest/poll/"+id,function(data){
            jsonResults = data.pollResponse;
    
        }).error(function(){ 
            alert('returning error');
            callback("error");
        }).success(function(){
            alert('returning data '+jsonResults);
            callback(jsonResults);  // is lost after it's returned
        });
    };
    
    pollServiceForInfo(id, function(json) {
        alert(json);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to return a JSON object from an aspx page using Jayrock.JSON.
I'm trying to get a json serialized object from within an MVC user control
I'm trying to get the JSON format string from my custom object by using
I'm trying to read data from a service (returns JSON object) and create an
I'm trying to return json content read from MySQL server. This is supposed to
i am noticing a error when im trying to return json from a fetchAll.
I'm trying to return a JSON response from a Django view call via an
I am trying to get the models from django but nothing is showing up.
I'm trying to serialise an object to return it in Json format in order
I'm currently trying to return multiple JSON objects in one of my controllers as

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.