Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to Use Exception Handling in Javascript / Jquery function

My function is like this

// JavaScript Document


$('#MyFormSubmit').submit(function(){

    try{
    /* Ajax - 1 */
    $.ajax({
           url: 'Any',
           type: 'GET',
           data: 'ID',
           contentType: "application/json;charset=utf-8",
           success: function (data) {
               alert("Success - 1");               
            },
            error: function (x, y, z) {
                alert("Error - 1");
            }
        });

    /* Ajax - 2 */
    $.ajax({
           url: 'Any',
           type: 'GET',
           data: 'ID',
           contentType: "application/json;charset=utf-8",
           success: function (data) {
               alert("Success - 2");               
            },
            error: function (x, y, z) {
                alert("Error - 2");
            }
        });

    /* Ajax - 3 */
    $.ajax({
           url: 'Any',
           type: 'GET',
           data: 'ID',
           contentType: "application/json;charset=utf-8",
           success: function (data) {
               alert("Success - 3");               
            },
            error: function (x, y, z) {
                alert("Error - 3");
            }
        });
    }
    catch(e){
        alert(e);   
    }

});

where 1 get first error, then I want stop execution of next ajax ? How to do ?

like image 417
Vikram Avatar asked Jul 18 '26 14:07

Vikram


2 Answers

Within the error block of first ajax call, just add return false.

error: function (x, y, z) {
                alert("Error - 1");
                return false;
            }
like image 81
Praveen Avatar answered Jul 20 '26 03:07

Praveen


Plese Write Code in this way

function fnMyFunction1(){
  try{
   if(success){
     // call fnMyFunction2
   }
   else{
     // Show Error message
   }
  }
  catch(e){
     // Show Exception message
  }
}
like image 22
Vikram Kumar Avatar answered Jul 20 '26 04:07

Vikram Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!