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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:40:47+00:00 2026-05-17T00:40:47+00:00

I’m sure this is simple, but I’m tripping up over a scope issue in

  • 0

I’m sure this is simple, but I’m tripping up over a scope issue in Javascript. I’m doing the following:

$.get(url, function (data){console.log(data);});

This part works fine – I see the string that I want appear in the console. But what I actually want is to take that data variable and place it in a string. Something like this (doesn’t work):

string = $.get(url, function (data){return data;});

This gives string a value of [object XMLHttpRequest].

What am I missing?

Oops

Yes, I’m missing the fact that Ajax is, as the name says, asynchronous. Thanks for giving me the slap on the forehead I needed, everyone.

Afterword

The reason I wasn’t simply working with the results inside the callback function itself is that I actually need to do multiple AJAX requests every few seconds, gather up the data, and append them all to the page when they’re all done.

I’ve now got that working with a little closure function – the requests are done in a loop, and the data is passed to the closure. When the timer goes off, I call the closure function with no arguments, which tells it "append your previous batch of data to the page (the requests are surely complete by now), clear your cache, and prepare to start receiving new data from the AJAX callbacks in my loop."

Which (hopefully) shows that I’m not an idiot after all. 🙂

  • 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-17T00:40:48+00:00Added an answer on May 17, 2026 at 12:40 am

    .get() sends an AJAX GET HTTP request and returns immediately meaning that you cannot assign the result to a variable simply because the result is only available in the success callback (the anonymous function you are passing as second argument). In this callback you could call another functions and pass the data as variable. That’s just how AJAX works. The A stands for asynchronous.

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

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I need to clean up various Word 'smart' characters in user input, including but
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a JSP page retrieving data and when single or double quotes are
I've got a string that has curly quotes in it. I'd like to replace

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.