简体   繁体   中英

jQuery.ajax fail handler not called

According to the jQuery documentation :

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

But when I use the following code:

$.ajax({
    type: 'GET',
    url: '@Url.Page("Create", "History")',
    contentType: 'application/json',
    dataType: 'json',
    data: { 'name': value },
    done: function (response) {
        alert('done' + response);
    },
    fail: function (response) {
        alert('fail' + response);
    },
    error: function (response) {
        alert('error' + response);
    },
    success: function (response) {
        alert('success' + response);
    },
});

If my Razor Pages handler throws an exception, the error response is shown. (And nothing is shown if I delete the error handler.)

I don't mind using fail if it's preferred. But if it's not going to be called on errors it won't do much good.

UPDATE : I can also see that the done handler also doesn't get called unless I change it to success .

UPDATE : It looks like I'm using jQuery 3.3.1.

just by using jQuery documentation , it's been a while I don't use jQuery, I try the most to avoid it, there are so much better tools these days

 jqXHR = $.ajax({ type: 'GET', url: '/', contentType: 'application/json', dataType: 'json', data: { 'name': 123 } }).promise(); jqXHR.done(function( data, textStatus, jqXHR2 ) { console.log('TEST 1: done', textStatus) }); jqXHR.fail(function( jqXHR2, textStatus, errorThrown ) { console.log('TEST 1: fail', textStatus) }); jqXHR.always(function( data, textStatus, errorThrown ) { console.log('TEST 1: always', textStatus) }); jqXHR.then( function( data, textStatus, jqXHR2 ) { console.log('TEST 1: then1', textStatus) }, function( jqXHR2, textStatus, errorThrown ) { console.log('TEST 1: then2', textStatus) } ); // or you can write $.ajax({ type: 'GET', url: '/', contentType: 'application/json', dataType: 'json', data: { 'name': 123 } }).done(function(data, textStatus) { console.log( "TEST 2: success", textStatus ); }).fail(function(data, textStatus) { console.log( "TEST 2: error", textStatus ); }).always(function(data, textStatus) { console.log( "TEST 2: complete", textStatus ); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM