0

I do some asynchronous REST API calls in my JS:

async function api(args)
{
    var response = await $.post( "/api", args)
    .fail(function(xhr, status, error)
    {
       console.log(xhr);
       console.log(status);
       console.log(error);
       errorLog(".fail in api: " + status + " - " + error + " - " + xhr.status + " - " + xhr.statusText + " - " + xhr.responseText);
    });
}   

In case something is wrong, I want to log out the root cause. I tried so many things, but it is impossible to log out the error reason. The only thing I have in my log files is:

Object {
readyState: 0, getResponseHeader: getResponseHeader(key), getAllResponseHeaders: getAllResponseHeaders(),
setRequestHeader: setRequestHeader(name, value),
overrideMimeType: overrideMimeType(type), statusCode:
statusCode(map), abort: abort(statusText), state:
state(), always: always(), catch: catch(fn), … } 

.fail in api: error -  - 0 - error - undefined

What I am doing wrong? Is this maybe because its inside an async method? Nothing explains me the actual root cause, I only get "error", "undefinied", "0" and " ".

Additional Info: I have that code online, it only happens for Firefox users (I log out the browser engine, its always "Mozilla/5.0")

dontspeak
  • 37
  • 6
  • Debug your code with console.log() and look at the xhr what returns exactly. And also look here: [link](https://stackoverflow.com/questions/21897398/how-do-i-debug-jquery-ajax-calls) – KOMODO Sep 29 '22 at 12:10
  • Does this answer your question? [How do I debug jquery AJAX calls?](https://stackoverflow.com/questions/21897398/how-do-i-debug-jquery-ajax-calls) – angel.bonev Sep 29 '22 at 12:16
  • "*maybe because it's inside an async*" - then try without the async/await as see if that fixes it... – freedomn-m Sep 29 '22 at 12:16
  • @KOMODO I added the console output to the question – dontspeak Sep 30 '22 at 12:12
  • @angel.bonev unfortunately not, because that thread just shows how to execute the fail/error method in general. I already do that – dontspeak Sep 30 '22 at 12:13
  • @freedomn-m its a singe page application with background REST calls. must be asychronous. But anyway its only an assumption – dontspeak Sep 30 '22 at 12:13
  • How is api() called? What triggers it? – epascarello Sep 30 '22 at 12:20
  • *must be async* - I think you've misunderstood what `async`/`await` and ajax do. `$.post` is *already* asynchronous, - all your doing is making the call to *initiate* the async call async. It's like `var x = await "123"`. – freedomn-m Sep 30 '22 at 13:17

0 Answers0