Answer the question
In order to leave comments, you need to log in
How to pass a parameter from a nested function?
I don’t know, maybe the question is idiotic, but I can’t find a solution, maybe I don’t know how to search. There is some wrapper for standard ajax jquery function
var flag ;
function query(data, mid, elem){
$.ajax({
type: 'POST',
url: path_method,
data:{ 'data': data, 'm':mid },
beforeSend: function(){
loading(elem, 'start');
},
success: function(server_response){
var data = eval("("+server_response+")");
loading(elem, 'stop');
if(data.status){
status(true, data.msg)
flag = true; // в случае успешного выполнения запроса
}else{
status(false, data.msg)
flag = false; // в случае если вернулась ошибка
}
},
error: function(err, texterr){
status(false, 'error_ajax'+texterr)
}
})
return flag; // после выполнения функции flag возвращается как undefined
}
Answer the question
In order to leave comments, you need to log in
read about asynchrony in JavaScript
at the moment when the request is executed and success is called, it return flag;
will work for a long time,
you can pass callback to the function
function query(data, mid, elem, callback) {
$.ajax({
/* ... */
success: function (server_response) {
/* ... */
if (data.status) {
status(true, data.msg)
callback(true);
} else {
status(false, data.msg)
callback(false);
}
},
error: function (err, texterr) {
status(false, 'error_ajax' + texterr)
}
});
}
query(data, mid, elem, (flag) => {
console.log(flag);
});
function query(data, mid, elem) {
const executor = (resolve, reject) => {
$.ajax({
/* ... */
success: function (server_response) {
/* ... */
if (data.status) {
status(true, data.msg)
resolve(true);
} else {
status(false, data.msg)
resolve(false);
}
},
error: function (err, texterr) {
status(false, 'error_ajax' + texterr)
reject();
}
});
};
return new Promise(executor);
}
query(data, mid, elem).then((flag) => {
console.log(flag);
});
(async () => {
const flag = await query(data, mid, elem);
console.log(flag);
})();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question