Nell'applicazione JavaScript front-end, faccio una richiesta ajax per recuperare i dati dal server. Non appena ho i dati, voglio restituire quella informazione alla vista.Come restituire i dati dalla funzione di successo di ajax?
var view_data;
$.ajax({
url:"/getDataFromServer.json",
//async: false,
type: "POST",
dataType: "json",
success:function(response_data_json) {
view_data = response_data_json.view_data;
console.log(view_data); //Shows the correct piece of information
return view_data; // Does not work. Returns empty data
}
});
// return view_data; --> Keeping the return outside of the ajax call works but then I need to make my ajax call synchronous in order for this return clause to be executed after the ajax call fetches data.
Come faccio?
È necessario aggiungere il tag jquery al post .. – Superdrac
si assegna 'view_data = response_data_json.view_data; 'quindi perché non si utilizza' view_data' perché lo si sta restituendo in metodo di successo. – ozil
può essere questo può aiutarti http://codingbin.com/get-return-data-ajax-call/ – MKD