0

I need to create a javascript variable that acts the same as if I hard coded

var test = [{"first" : "second"}];

and so on. However I need to load the data from an external, local .json file and set that data equal to the variable. I've done TONS of different attempts such as

var test;
jQuery.ajax({
        'async': false,
        'global': false,
        'url': "sequence.json",
        'dataType': "json",
        'success': function (data) {
            test = data;
        }
    });

However in all cases, the original test variable is never set as if it were hard coded to a JSON object. Often, I'm unable to even set the value of test at all. What would be a good way to go about this?

2
  • can you set a breakpoint inside a success callback and see if ajax call invokes it? Commented Nov 6, 2013 at 8:07
  • 1
    This should work. Keep in mind that test is null until the data loads. So if you're console.loging it –– or running any code that depends on it –– prior to success being called, you get null. Commented Nov 6, 2013 at 8:11

1 Answer 1

1

if your ajax response is like data = [{"first" : "second"}]; then you can get you value like below. But json format is different then you have to specify your format first.

    var test;
    jQuery.ajax({
            'async': false,
            'global': false,
            'url': "sequence.json",
            'dataType': "json",
            'success': function (data) {
                data = [{"first" : "second"}];
                test = data;
                for(var i in test){
                console.log(test[i].first);
                alert(data[i]);
                }
            }
        });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.