0

I'm a newbee to JS , JSON and AJAX. I'm using it to develop a simple apps in my SAP env. I'm bit struck in converting the AJAX response to java array. What is have in the code is:

function addTable()
{
    var urls = new Array();
    $(document).ready(function ()
    {
        var params = getURLParam().split('?');
        $.post("GetBayDetails.htm", {url: getURLParam(), params: params[1]})
                .done(function (data)
                {
                    var url = $.parseJSON(data);
                    urls.push(JSON.parse(url));
                    $.each(url, function (i, v)
                    {
                        push.urls[i] = v.bay;
                    });
                });
    });
    alert(urls[2]);
}

but if I loop through "URLS" I do not see any value appended to the array. Please can anyone provide some help to get this fixed?

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
  • You're already parsing the response with `$.parseJSON`, you shouldn't need to call `JSON.parse` on it again. – Barmar Mar 30 '15 at 22:01
  • Please post an example of what the response JSON looks like. – Barmar Mar 30 '15 at 22:03
  • Hi, json looks like {"bay": "B01", "url": "thtrack1"}, {"bay": "B02", "url": ""}, {"bay": "B03", "url": "not defined"}, {"bay": "B04", "url": ""}, {"bay": "B05", "url": "colm2"}, {"bay": "B06", "url": ""}, – Prabhu Sethupathy Mar 30 '15 at 22:17
  • That's not valid, an array needs `[ ... ]` around it. Please post the actual JSON in the question. – Barmar Mar 30 '15 at 22:21
  • the methods in SAP returns the json as: [{"bay": "B01", "url": ""}, {"bay": "B02", "url": ""}, {"bay": "B03", "url": ""}, {"bay": "B04", "url": ""}, {"bay": "B05", "url": ""}, {"bay": "B06", "url": ""} ....] – Prabhu Sethupathy Mar 30 '15 at 22:24
  • if i do an alert(v.bay) i could see the values but not when mapping to an array....is something wrong in my code? – Prabhu Sethupathy Mar 30 '15 at 22:58

1 Answers1

0

Try this. My changes are:

  1. Use the "json" dataType argument to $.post so it parses the response automatically.

  2. There's no need to use $(document).ready() inside a function that you call on demand. This is only needed when performing initial actions that have to wait for the DOM to be loaded.

  3. It's not necessary to call JSON.parse(url), as this is already parsed.

  4. The correct way to add to the urls array is urls.push(v.bay).

  5. The preferred way to initialize an array is with [], not new Array().

  6. alert(urls[2]) needs to be in the .done() function. Otherwise you're alerting before the AJAX call has completed.

function addTable() {
    var urls = [];
    var params = getURLParam().split('?');
    $.post("GetBayDetails.htm", {url: getURLParam(), params: params[1]}, "json")
        .done(function (url) {
        $.each(url, function (i, v) {
            urls.push(v.bay);
        });
        alert(urls[2]);
    });

}

DEMO

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thnk u barmar, unf this doesn't work either for me....just taking more time to identify the issue here! – Prabhu Sethupathy Mar 30 '15 at 23:52
  • thkx it okay now but if i have to refer the array outside the "post" it doesn't work...anything to be done for that? pls advise – Prabhu Sethupathy Mar 31 '15 at 00:05
  • when i tried it just before the closing paranthesis of the addTable i get the msg as undefined....what shoud i do to make it visible out side the post? pls let me know – Prabhu Sethupathy Mar 31 '15 at 00:08
  • Everything that depends on an AJAX call must be done in the callback function -- see my change #6. http://stackoverflow.com/questions/23667086/why-is-my-variable-undefined-after-i-modify-it-inside-of-a-function-asynchron?newsletter=1&nlcode=97716%7c4ba7 – Barmar Mar 31 '15 at 00:16
  • See also http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – Barmar Mar 31 '15 at 00:16
  • may i ask a question in this post please? is it possible to access the dynamic table creation within the callback? – Prabhu Sethupathy Mar 31 '15 at 00:32
  • Yes, you can do pretty much anything you want in the callback. – Barmar Mar 31 '15 at 00:35