tags:

views:

33

answers:

1
+1  Q: 

$.ajax try catch

I have an $.ajax call that includes both a success and error condition:

$('input[name="StateName"]').live('change', function() {
    var StateID = $(this).parents('tr').attr('id');
    var StateName = $(this).val();
    $.ajax({
        url: 'Remote/State.cfc'
        ,type: "POST"
        ,data: {
            'method': 'UpdateStateName'
            ,'StateID': StateID
            ,'StateName': StateName
        }
        ,success: function(result){
            if (isNaN(result)) {
                $('#msg').text(result).addClass('err');
            } else {
                $('#' + result + ' input[name="StateName"]').addClass('changed');
            };
        }
        ,error: function(msg){
            $('#msg').text('Connection error').addClass('err');
        }
    });
});

Q: Should I also wrap this in a try/catch?

+3  A: 

There is no need for try catch, as that adds to redundancy.

on jQuery's side, they've done quite well on the error catching within their methods. As for your code, IMHO I don't see the need.

thephpdeveloper
He's write jQuery will catch any error. Note that in jQuery 1.4, when you are returning invalid JSON it will throw an error that has to be caught with a try/catch in your success function.
Drew