S
S
SvetlanaDubovik2017-07-21 12:34:14
JavaScript
SvetlanaDubovik, 2017-07-21 12:34:14

Data is not parsed. Why?

Hello. Can you please tell me the following function

function showPredecessor(urlValue) {
            var predecessor = [];
    predecessor = (collectData(urlValue, 'СвПредш')); (1)
}

It is assumed that this function receives data from urlValue , forms DOM elements from them and displays them on the screen. I have about 5 such similar functions. I wanted to write a function that will parse a JSON file. It is presented below
function collectData(urlv, field) {
    $.getJSON(urlv, function (data) {
        var arr = [];
        if (data.response[field] != null) {
            for(var i =0; i < data.response[field].length; i++)
                arr.push(getData(data.response[field]));
        }
        return arr;
    });
}

When I call showPredecessor(url), it goes to collectData in line (1), but everything that is done is in $.getJSON(urlv, function (data) { }); debugger ignores. It just jumps and doesn't work. With what such behavior can be connected?? Or, in principle, it is impossible to make the execution of Ajax requests in a separate function??

PS Found that it starts, but only after it encounters an error in the showPredecessor () function. It turns out that I can't call an Ajax request when I need to??

Answer the question

In order to leave comments, you need to log in

5 answer(s)
R
riot26, 2019-02-06
@Vankiro20

https://github.com/kamranahmedse/developer-roadmap

M
McBernar, 2019-02-09
@McBernar

When you're done with the book, it's time to start typesetting.

W
webenbond, 2019-02-06
@webenbond

after html / css, it’s just js that’s worth studying, well, you can also study bootstrap-like frameworks before that

S
Stanislav Shendakov, 2019-02-09
@shindax

Chris Aquino, Todd Gandy
Front-end. Client development for professionals. Node.js, ES6, REST.
Despite the title, I find this book to be one of the best for beginners.

A
Artem, 2017-07-21
@alfaslash

The function
is asynchronous, which is why the debugger "jumps" to the next code. The callback for getJSON is called later, when the response from the server has already arrived. Try putting a breakpoint on
or below the line.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question