Link to home
Start Free TrialLog in
Avatar of sherbug1015
sherbug1015Flag for United States of America

asked on

Passing a variable out of an ajax call

I have some Jquery like this that is not working.
var roomid = 0;

           $.ajax({
                     type: "POST",
                       url: "/CMSPages/WebService.asmx/GetChatRoomId",
                       data: {RecipientId : recipientid},
                      success: function (data) {
                         var encodedHtml = data.children[0].innerHTML;
                         var decodedHtml = decode(encodedHtml);
                        
                        }
            });
           
            roomid = decodedHtml;

decodedHtml has not value outside of the ajax call.  How can I get that value out to the roomid variable.

Thanks.
Avatar of Francisco Igor
Francisco Igor
Flag of Canada image

DecodedHtml is a private variable inside function.

Set the roomid value directly into response function:
(This is evaluated asynchronously after POST is done)

var roomid = 0;

           $.ajax({ 
                     type: "POST",
                       url: "/CMSPages/WebService.asmx/GetChatRoomId", 
                       data: {RecipientId : recipientid},
                      success: function (data) { 
                         var encodedHtml = data.children[0].innerHTML;
                         var decodedHtml = decode(encodedHtml);
                          roomid = decodedHtml;
                        } 
            });
            

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Russ Suter
Russ Suter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial