简体   繁体   中英

Why is my AJAX call success function not firing a java script alert?

$.ajax({
   url: '../api/notifications/deleteNotification?userId=' + userId + '&notificationId=' + notificationId,
   type: 'DELETE',
   success: function()
   {
       CreateNotificationTree(userId);
       alert('Delete successful.');
   },
   failure: function()
   {
       alert('Delete failed.');
   }
});

The function CreateNotificationTree(userId); that is inside the success function of the ajax call above DOES fire. However, the Alert is not firing after. Does anybody know why? I have tried to use multiple browsers as well.

EDIT - found out I'm running into this error when the AJAX call executes:

Uncaught TypeError: Cannot read property 'uid' of undefined kendo.web.min.js:23
(anonymous function) kendo.web.min.js:23
p.extend.each jquery.min.js:2
p.fn.p.each jquery.min.js:2
g.extend._attachUids kendo.web.min.js:23
g.extend.init kendo.web.min.js:22
(anonymous function) kendo.web.min.js:9
p.extend.each jquery.min.js:2
p.fn.p.each jquery.min.js:2
$.fn.(anonymous function) kendo.web.min.js:9
CreateNotificationTree NotificationsTreeView.js:17
(anonymous function) NotificationsTreeView.js:60
k jquery.min.js:2
l.fireWith jquery.min.js:2
y jquery.min.js:2
d

Log the error to your console.

You do not see the alert if ajax fails method as jQuery does not identify the failure method.

Use a error callback to log the error.

Also use console.log instead of alert which is annoying and stops the flow of execution

failure: function(){
   alert('Delete failed.');
}

supposed to be

error: function(){
   alert('Delete failed.');
}

And use done and fail instead of success and error callbacks as the latter as deprecated as of version 1.8

$.ajax({
    url: '../api/notifications/deleteNotification?userId=' 
               + userId + '&notificationId=' + notificationId,
    type: 'DELETE'
}).done(function () {
    CreateNotificationTree(userId);
    console.log('Delete successful.');
}).fail(function (jqXHR, status, error) {
    console.log("Error : " + error);
});

Use the arguments that are passed to the callbacks and you ll be able to pinpoint the error.

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