Store global Ajax data return

Asked

Viewed 85 times

0

I am currently using ajax making several different requests for queries.

Ex:

$.ajax({
  method: "POST",
  url: "getData.php",
  data: { nome:nome, endereco: endereco }
})
  .done(function( data ) {
var dados = data;
    //aqui faço todas validações que preciso
  });

However, when I need to do a new validation differently, out of return . done I don’t have access to 'data' from data.

I wondered if I could somehow make this requisition ajax and store the data to not need to each need to validate a new request.

  • 1

    The block .done() is executed after the request when no error occurs. If the goal is to validate something before request, implement a block beforeSend. Details in the ajax documentation: http://api.jquery.com/jquery.ajax/

  • right, see what I need would be something like storing what comes from the return of ajax in a variable and access it outside the scope .done(). Ex: in the above ajax I have variable data, after closing the done tag I can no longer access that variable. If I have it I need more below validate a new example field compare if($("#enderecob") == dados.endereco) { // tratar aqui } , this out of done, it doesn’t work, there’s some way to do it?

  • 1

    It has as if that if is inside a function.

  • Got it, is that I haven’t seen any such example, and in my case today every time I need a PHP information in JS I’m making a new ajax request to get the data to handle the information. I looked for some information in this sense and found something that ajax and assicronomo and does not allow to work globally only within the scope.

No answers

Browser other questions tagged

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