I have the following jQuery code that our back end guy came up with and he has asked me for help on it. The issue is that the value for the closeMsg variable is supposed to be set on either the success or the failure of the Ajax post but it bizarrely is not set unless a subsequent alert is set to display it. Then that alert doesn't display but everything after that first alert does. Here is the code and I'm adding in comments around the alert to explain it a little better.

$('#confirmDeleteDelete').click(function (e) {
    var deleteUrl = baseUrl + 'delete/' + $('#confirmDeleteDelete').attr('name');
    var closeMsg = null;
    var closeUrl = null;

        url: deleteUrl,
        type: 'POST',
        //data: {},
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            closeMsg = 'Successfully deleted record';
            closeUrl = baseUrl;
        error: function (xhr, status, error) {
            closeMsg = 'Could not delete record, please try again';
            closeUrl = null;


    alert(closeMsg); // Here's the first alert; when it displays it shows a value of "null"
    alert(closeMsg); // Here's the second alert; when it displays it shows a value of "Successfully deleted record" (assuming a successful Ajax post)
    $('#promptDeleteMsg').after('<p id="#confirmDeleteMsg" />').text(closeMsg).show(); // This is where we actually want the value to show up but it doesn't unless at least the first alert is in place
    $('#confirmDeleteClose').attr('name', closeUrl);

Any ideas? If it helps anything there doesn't seem to be any problem capturing the associated closeUrl variable value.