0

I'm performing some ajax requests in a js.

Everything is working perfectly ... however, when a code 500 is returned by the server, the application does not continue its flow.

how is it possible to make so that even when an exception 500 is returned the code continues?

   async function isValid(data) {

            let result = true;

            await $.ajax({
                url: '@Url.Action("GetValid", "User")',
                cache: false,
                data: data,
                success: function (data) {
                    result = data;
                },
                error: function (data) {
                    result = false;
                    // break code ... returned code 500
                }
            });

            return result;
        }

Result in Console

Failed to load resource: the server responded with a status of 500 ()
index.html:45 Uncaught (in promise) Object
Emiry Mirella
  • 567
  • 1
  • 7
  • 21

1 Answers1

0

This should work for you. In order to use async await, you should use a try catch block to properly handle errors which apparently the $.ajax will just error out on.

The console.log statements are there to show you what the results and errors would be. You should remove them before putting in a codebase

 async function isValid(data) {

   let result = true;

   try {
     await $.ajax({
       url: '@Url.Action("GetValid", "User")',
       cache: false,
       data: data,
       type: 'POST',
       success: function(data) {
         result = data;
         return result
       },
       error: function(data) {
         result = false;
         console.log({
           result
         })
         // break code ... returned code 500
       }
     })
   } catch(e) {
    console.log({e: e})
    console.log(result)
    return result
   }

 }

 isValid()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
superjisan
  • 1,604
  • 14
  • 15