Uncaught Referenceerror problem: answer is not defined

Asked

Viewed 1,001 times

0

To with a problem, in sending data using ajax, my function always returns that same error: Uncaught Referenceerror: reply is not defined;

Function that passes the data::

function salvaDadosHistorico(username,valorDado,resultado,nunSorteados){

    dados = 'username='+username+',valorDado='+valorDado+',resultado='+resultado+',nunSorteados='+nunSorteados;


    resp = chamadaAjax(dados,'core/historico-control.php','POST');
    return resp;

}

function giving you the error:

function chamadaAjax(dados,rota,type) {
    $.ajax({
        type: type,
        data: dados,
        url: rota,
        success: function (res)
        {
            resposta = res;
        },
        error: function(res)
        {
            resposta = res;
        } 
    })
    return resposta;
}
  • 6

    When it comes to this line return resposta;, the variable resposta does not exist. What is inside AJAX is process after.

1 answer

2


The problem is that your request ajax is an asynchronous function, so the response variable does not yet exist because the ajax is still running. A possible solution is to use the $.when() function of jquery to wait for promise.

function chamadaAjax(dados,rota,type) {
    return $.ajax({
        type: type,
        data: dados,
        url: rota,
    })
}


//ai você pode chamar sua função salvardadoshistoricos passando a função de sucesso e erro ou editar da forma que achar melhor
function salvaDadosHistorico(funcaoSucesso,funcaoErro){

    dados = {}
    chamadaAjax(dados,'https://viacep.com.br/ws/01001000/json/','GET').done(funcaoSucesso).fail(funcaoErro);
}

function funcaoSucesso(response) {
console.table(response);
}

function funcaoErro(err) {
    console.log(err);
}

salvaDadosHistorico(funcaoSucesso,funcaoErro);

There are several ways, such as the use of await for example. Look for asynchronous functions.

Here is the jQuery.when function documentation link()

  • I tested this code and it returns me Cannot read Property 'done' of Undefined

  • Hello @Lukastakahashi, I made a code change to a URL that I had access to, I tested it here and it worked normally, if I can validate there again. Don’t forget to import Jquery first. Big Hug.

  • Bgd for help, at first I wish I didn’t have to create a new function to run the data, just return the response of the ajax call as callback of the saved functionDadosHistorico(). But I gave up this idea and his answer helped a lot to solve this problem.

  • @Lukastakahashi, you can return too, for example by doing salvaDadosHistorico(foo,bar) { return chamadaAjax(dados,url); } Ai you treat the promisse in your other function. In your initial function you can use anonymous functions as well. The important thing is to always treat this return either with when or with await (await only works in newer browsers). There is still the option of an async ajax (but this is not recommended). And there are still other ways rs. Success ai!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.