0

So trying to get two JSON requests to happen after each other, and seems like the javascript does the first request, but never even attempts the second one.

So, first one asks for colors, the second one asks for values, the code looks like this

(function() { 
    var mapKeyUrl = "/GenMap/getcountry/mapdata"
    $.getJSON(mapKeyUrl, {
        regsel: "${regsel}",
        variable: "mapcolor"
    })
    .done(function( coldata ) { 
        console.log( "JSON Data: " + coldata[1].value );
        (function() {
            mapKeyUrl = "/GenMap/getcountry/mapdata"
            $.getJSON(mapKeyUrl, {
                regsel: "${regsel}",
                variable: "mapkey"

            })
            .done(function( mapdata ) {
                console.log( "JSON Map:" + mapdata)
            })
        })

    });
})();

Both requests return valid JSON data, so mapcolor for example:

[{"class":"genmap.Mapconf","id":55,"map":"0","reg":"FATCA","value":"#F0F0F0","variable":"mapcolor"},{"class":"genmap.Mapconf","id":56,"map":"1","reg":"FATCA","value":"#66CCFF","variable":"mapcolor"},{"class":"genmap.Mapconf","id":57,"map":"2","reg":"FATCA","value":"#0000FF","variable":"mapcolor"},{"class":"genmap.Mapconf","id":58,"map":"3","reg":"FATCA","value":"#CC66FF","variable":"mapcolor"},{"class":"genmap.Mapconf","id":59,"map":"4","reg":"FATCA","value":"#9900CC","variable":"mapcolor"},{"class":"genmap.Mapconf","id":60,"map":"5","reg":"FATCA","value":"#7D7D7D","variable":"mapcolor"}]

And mapkey returns a similar list.

Any suggestions, need to run three of these, and one I have all the data I need to recombine it.

4
  • have you tried stepping through the code to see what happens? Commented Oct 7, 2014 at 12:25
  • My guess is that you end up in a fail callback in your first call. Try adding one Commented Oct 7, 2014 at 12:27
  • You're never calling the second function. At the end of the main function you have })(); with the () to call it, but at the end of the inner function you just have }) Commented Oct 7, 2014 at 12:28
  • The problem is that you wrap second AJAX request with anonymous function which never invoked. Just remove these wrapping lines and the code will work Commented Oct 7, 2014 at 12:32

2 Answers 2

2
(function() {
    mapKeyUrl = "/GenMap/getcountry/mapdata"
    //...
})

You define a function but you never call it, which is why it doesn't run.

Since there is no point in wrapping it in a function anyway, just omit it:

.done(function( coldata ) { 
    console.log( "JSON Data: " + coldata[1].value );

    mapKeyUrl = "/GenMap/getcountry/mapdata"
    $.getJSON(mapKeyUrl, {
        regsel: "${regsel}",
        variable: "mapkey"

    }).done(function( mapdata ) {
        console.log( "JSON Map:" + mapdata)
    });

});
Sign up to request clarification or add additional context in comments.

4 Comments

Well spotted and thank you so very much. Been swearing about JSON for a while, so thought it was another wonderful JSON special, turned out it was me forgetting to add var :)
OK, so I changed it to be a var, but that does not seem to do the trick, or am I missing the point? Still only one response
@vrghost Updated my answer. It helps to learn the basics before doing complex AJAX stuff. ;) developer.mozilla.org/en/learn/javascript
@jglitch: I would have loved to not need to use JSON, but needed a way to get data from the backend to the browser, and that was the only way I could find to do it:)
1

please use this inside function.@jgillich is right.you can call it like as you done in parent function.

(function() {
        mapKeyUrl = "/GenMap/getcountry/mapdata"
        $.getJSON(mapKeyUrl, {
            regsel: "${regsel}",
            variable: "mapkey"

        })
        .done(function( mapdata ) {
            console.log( "JSON Map:" + mapdata)
        })
    })();

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.