function $.Ajax() return value?

Asked

Viewed 14,892 times

11

that is the code:

var res = VerFileRepetidoBancoAjax( "teste" );
console.log("res="+res);

function VerFileRepetidoBancoAjax( str ){
 $.ajax({
    url:    "caminho.",
    type:   "get",
    dataType:"json",
    data:   "dado="+ str,
    async: false,

    success: function( data ){
        return data;           
    }
});
}

In this precise case the return value of the Ajax function, which always returns Undefined.
This function returns a small Json object which will then be manipulated elsewhere.

For now, to resolve this issue I used a public variable, follows:

/* declaro uma variavel publica */
var res ;
VerFileRepetidoBancoAjax( "teste" );
console.log("res = "+res);

function VerFileRepetidoBancoAjax( str ){
 $.ajax({
    url:    "caminho.",
    type:   "get",
    dataType:"json",
    data:   "dado="+ str,
    async: false,

    success: function( data ){
        /* aqui coloca o OBJ dentro da variavel publica*/
        res = data;           
    }
});
}

There is a form within the $.ajax function itself();

  • What other type of language are you making an ajax call for? PHP? ASP? Your mistake is at url, because it is waiting for you to send the request to a function, or file, which in turn will have a Return

  • Actually the returned Json will be used within the javascript itself and by the way the language I am using is Java.

3 answers

5

You are returning the date inside the function, but do not return anything outside it, try it like this:

var res = VerFileRepetidoBancoAjax( "teste" );
console.log("res="+res);

function VerFileRepetidoBancoAjax( str ){
 var retorno;
 $.ajax({
    url:    "caminho.",
    type:   "get",
    dataType:"json",
    data:   "dado="+ str,
    async: false,

    success: function( data ){
        retorno = data;           
    }
});
return retorno;
}
  • Very well observed, Tks. I was wearing a public variable to cause the same effect as your answer gives me. Flw.

  • The secret is here: sycr: false , I also got a lot because of this mere detail =)

  • Synchronous requests have been marked as deprecated. You should avoid using them. https://answall.com/questions/48011/nos-newbrowsers-agora-n%C3%A3o-haver%C3%A1-mais-requisi%C3%A7%C3%B5es-s%C3%Adncronas

3

The value returned is in the parameter data of its role of callback in case of success:

success: function( data ){
    console.log("data ="+data );      
}

The call $.ajax itself is asynchronous by default (async: true), and its function VerFileRepetidoBancoAjax is not returning anything.

1

The use of synchronous ajax requests is discouraged, probably on your console will come out something like this: Synchronous xmlhttprequest should not be used on the main thread due to its detrimental effects to the user experience. For more information http://xhr.spec.whatwg.org/.

What can be done in these cases is to use callback, its function would look something like this:

function VerFileRepetidoBancoAjax( str, response, err ){
    $.ajax({
        url:    "caminho.",
        type:   "get",
        dataType:"json",
        data:   "dado="+ str,

        success: function( data ) {
            response(data);   
        },

        error: function (request, status, error) {
            err(request.responseText);
        }
    });
}

And I’d wear it like this:

 VerFileRepetidoBancoAjax( "teste", function (response) {
      console.log(response);
 }, function (error) {
     console.log(error);
 });

Browser other questions tagged

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