I have the following script which works as long as it is returned data:
$(document).ready(function() {
$.ajax({
type: 'POST',
url: 'notifications.asmx/GetNotifications',
data: '{strUser: "pqr"}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function( notifications ) {
if( notifications !== null ) { //<-- this makes no difference
alert( notifications.d[0].text ) ;
} else {
alert ( "null" );
}
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message) ;
}
});
});
If data is not returned, I get the error:
Uncaught TypeError: Cannot read property 'text' of undefined
I have tried wrapping it in various if statements checking for null values, but that does not make any difference.
It sounds like it’s
notifications.d[0]that is undefined so you should be checking that in your if statement rather thannotifications.