Suniel
Suniel

Reputation: 1499

check success and error in ajax

I have a button in jsp like this.

 <input class="submit_button" type="submit" id="btnPay" name="btnPay" value="Payment"  
 style="position: absolute; left: 350px; top: 130px;" onclick="javascript:payment();">

The javascript function calls the java servlet and the servlet calls the function "callprocedure()" when the button is clicked.

 <script>

     function payment()
    {

        var req = $.ajax({
        type: "POST",
        url: '/context/Servlet',
        success: function(result){
         //when successfully return from your java 
        alert('Payment done successfully...');
        }
        }, error: function(){
           // when got error             
           alert("sorry payment failed");            
        }
    });

    }      
</script> 

Now all works fine but my problem is to check the success or error in ajax. How can i check the success or error in ajax in my case.

Thanx in advance ...

Upvotes: 0

Views: 587

Answers (2)

mario
mario

Reputation: 477

Another way to do it (not very efficient though):

Check the returned value from servlet.

$.post('Servlet',paramName:paramValue,...},function(result) {
  if (result.substring(0,...)=='(expected result)'){ //success

  }else{ //error

  }
});

Hope it helps

Upvotes: 0

Adil Shaikh
Adil Shaikh

Reputation: 44740

You are doing that correctly, However you have syntax error in your code :

function payment() {
    var req = $.ajax({
        type: "POST",
        url: '/context/Servlet',
        success: function (result) {
           // alert data returned by jsp
            alert(result);
            alert('Payment done successfully...');
        },
        error: function (jqXHR,textStatus,errorThrown) {
            // when got error             
            alert("sorry payment failed :" + errorThrown);
        }
    });
}

you can use parameter passed to error callback to know the cause of error

More info

Upvotes: 2

Related Questions