I am using JQuery AJAX, using await. I do not want to use the success and error callbacks and I do not want to use the .then syntax.
When calling jquery ajax like so
const { data, textStatus, jqXHR } = await $.ajax(url, {...});
then the variables on the left are not populated.
How to call jquery ajax using await and still get data, textStatus and jqXHR (similar to the success callback)?
(Simply calling const data = await $.ajax(url, {...}); works but I need access to textStatusand jqXHR.)
you can get the 3 parameters you specified in the success option for the ajax function and do the destructuring on that anonymous function
let data, textStatus, jqXHR;
await $.ajax(url, {success: (dt, txt, jqx) => {
data = dt;
textStatus = txt;
jqXHR = jqx;
}});
or extract the function and do it like this
...
const success = (dt, txt, jqx) => {
data = dt;
textStatus = txt;
jqXHR = jqx;
}
...
await $.ajax(url, {success});
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With