$.ajax exception handling

by Anton

HTML

<button id="start">Test</button>
<p>Result: <span id="result">Press "Test"</span></p>
<p>Caught exception: <span id="exception">None</span> (see console for uncaught)</p>

JavaScript

$('#start').on('click', function() {
	console.log('click');
    try {
        $.ajax({
            url:'https://fiddle.jshell.net/',
            success: function (response) {
                $('#result').text('Success');
                throw 'Exception from success';
            },
            error: function () {
                $('#result').text('Error');
            }
        });
    } catch(e) {
    	$('#exception').html(e);
    }
});