簡體   English   中英

jQuery.ajax 未調用失敗處理程序

[英]jQuery.ajax fail handler not called

根據jQuery 文檔

棄用通知:自 jQuery 3.0 起,jqXHR.success()、jqXHR.error() 和 jqXHR.complete() 回調已被刪除。 您可以改用 jqXHR.done()、jqXHR.fail() 和 jqXHR.always()。

但是當我使用以下代碼時:

$.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);
    },
});

如果我的 Razor Pages 處理程序拋出異常,則會顯示錯誤響應。 (如果我刪除錯誤處理程序,則不會顯示任何內容。)

如果首選,我不介意使用fail 但是,如果它不會因錯誤而被調用,它就不會有太大的好處。

更新:我還可以看到done處理程序也不會被調用,除非我將其更改為success

更新:看起來我正在使用 jQuery 3.3.1。

只是通過使用jQuery 文檔,我已經有一段時間不使用 jQuery,我盡量避免它,現在有很多更好的工具

 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM