user7441202
user7441202

Reputation:

AJAX not working in JQUERY confirm dialogue

I'm trying to call an ajax function after the user confirms a JQUERY confirm dialogue. But after putting in the confirm part. It gives me an error and doesn't work. Please note that the ajax function works fine when placed outside the confirm dialogue.

My code is

 function approveApp(intAppId) {      
         $.confirm({
             title: 'Are you sure you want to approve this application?',
             content: '',
             buttons: {
                 confirm: function () {
                     var test = intAppId;
                     alert(test) //This alerts the word 'Undefined'
                     var intAppId = intAppId;
                     $.ajax({
                         url: 'adminreport.aspx/approveApp',
                         type: 'POST',
                         data: JSON.stringify({ intAppId: intAppId }),
                         contentType: "application/json; charset=utf-8",
                         dataType: "json",

                         success: function (data) {
                             if (data.d) {
                                 alert("It worked!");
                             }
                         }
                     });
                 },
                 cancel: function () {

                 }
                 }
             }
         });

     }

Upvotes: 1

Views: 591

Answers (1)

James Thorpe
James Thorpe

Reputation: 32202

You're passing the parameter to the outer function as intAppId.

However, you're also declaring that variable in the inner function and using it to (presumably) hold the outer parameter:

var intAppId = intAppId;

This re-declaration is hiding the outer variable. You can just remove it entirely and just use the parameter in your ajax call.

Upvotes: 5

Related Questions