I am curious about the best practices for retrieving small data from the server. One example is using an ajax (or sjax) call to check for new notifications for a user.
function checkNewNotifs() {
$.ajax({
url: '/Home/CheckNewNotifications',
async: false,
success: function (data) {
if (data == 'True') {
$('#alert-icon').css('color', '#FF4136');
}
}
})
}
While this method works, I wonder if there is a more efficient way to accomplish this task. I am currently utilizing ASP.NET MVC 4/5 for context.
Edit:
For those new to ajax like myself, it is recommended to use .done()
for similar tasks instead of setting async to false. Here is an example:
function checkNewNotifs() {
$.when(
$.ajax({
url: '/Home/CheckNewNotifications',
success: function (data) {
//perform data manipulation
}
})).done(function() {
//update view accordingly
})
}
tl;dr async: false = not recommended