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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:37:46+00:00 2026-05-16T17:37:46+00:00

Some information – I’m creating an autocomplete which gets the data from a json

  • 0

Some information – I’m creating an autocomplete which gets the data from a json feed. The JSON part works fine and the result is valid.

When I’m obtaining it, I’m using json2.js and running JSON.parse. When I try and output it tells me that it (the object containing the parsed JSON text) is actually undefined.

If I run an alert on the object and then output it works. It’s probably something quite simple. But this is the bit that is confusing as it works fine if I alert the object

I know that it won’t work on everything, I’m just trying to get it working for now and I’ll improve it.

Thank you and if there is any more information I can provide I will.

The code

//sURL takes a search term that's passed into the function

var JSON_object = {};
var oRequest = new XMLHttpRequest();
var sURL  = "datalinkhere"+input.value;

oRequest.open("GET",sURL,true);
oRequest.setRequestHeader("User-Agent",navigator.userAgent);
oRequest.onreadystatechange = function () {
    if (oRequest.readyState == 4 && oRequest.status == 200)
    {
        JSON_object = JSON.parse( oRequest.responseText );
    }
};
oRequest.send(null);

suggestion(JSON_object,input);

function suggestion(inp,targetid)
{
    document.getElementById('autosuggest').style.display='block';
    document.getElementById('autosuggest').innerHTML=inp[1].namefield;
}
  • 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-16T17:37:47+00:00Added an answer on May 16, 2026 at 5:37 pm

    The problem ist not alerting the json or not it’s the concept of your code. Ajax requests work asynchronously, thus your oRequest.send call will not block until the data has been loaded, the data is loaded in the background.

    So you can have luck and the data is available when the next line (suggestion-call) and your code works or you will get an undefined var.

    You’ll have to write your code asynchronously: Put the suggestion-call direcly after the JSON.parse-call and all will work like a charm.

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

Sidebar

Related Questions

I am downloading some information from a JSON feed about countries, states and their
I'm taking some information from data base and i want to use it in
I've pieced together some information from other posts but I'm stuck. The first part
I need to show some information in a graph, the data is held in
Trying to read in some information from the google maps api into my application
I need to extract some information from not very complicated HTML pages in web.
I will send some information from one site to another site. I have an
I need to get some information from user by showing a JFrame I need
I want to send some information from PHP to Java. Why? Because I have
I need to send some information from my PC to an Arduino. I tested

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.