Returning Ajax request undefined

0

I'm studying a bit of Ajax and testing your requests. My question is because of the return, it is coming back undefined.

This is the function:

function recarregar()
{
    var jsonData = $.ajax({
        url: "http://xxxxxxx/o/Ano/1235",
        dataType: "text",
        method: "Post"
        async: false
    }).responseText;
    var teste = jsonData;
}


setInterval(recarregar(), 10000);

This is the return according to Postman

    
asked by anonymous 21.02.2018 / 15:25

1 answer

2
  

Note:   It is missing a comma between post "and async :

    method: "Post"
    async: false

Please do not use async: false , this is obsolete and in the future browsers will remove this functionality, this is because the synchronized mode usually freezes the browser, as opposed to asynchronous, as I explained in

And as described in link

Then change the approach to use callbacks , to understand what callback is, read this answer:

Then change the setInterval to setTimeout, so you will only call the next if the first one has been sent

function recarregar()
{
    $.ajax({
        url: "http://xxxxxxx/o/Ano/1235",
        dataType: "text",
        method: "POST"
    }).done(function (jsonData) {
         var teste = jsonData;
    }).always(function () {
         setTimeout(recarregar, 10000);
    });
}

Now about the reason for being% return% must be scope of variables, to pass access to the test variable you would have to either put it as global (in the scope of undefined ) and still add some kind of refresh into the specific DOM you want to change with the value, or else change the approach completely.

If you learn how to use callback well you will probably be able to adjust everything, if you can indicate where you want to put the value of window. I can adjust the code and give you some suggestion.

    
21.02.2018 / 17:53