0

Possible Duplicate:
jQuery: Return data after ajax call success

So I'm calling a Sharepoint service and trying to return some values for use elsewhere and I'm trying to declare a window variable to do so. Seems simple but the window variables are undefined outside the responseXML functions.

userName = "";

var soapEnv =
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:tns='http://schemas.microsoft.com/sharepoint/soap/'> \
    <soap:Body> \
        <GetUserProfileByName xmlns='http://microsoft.com/webservices/SharePointPortalServer/UserProfileService'> \
            <AccountName></AccountName> \
        </GetUserProfileByName> \
    </soap:Body> \
</soap:Envelope>";

$.ajax({
    url: "/_vti_bin/userprofileservice.asmx",
    type: "POST",
    dataType: "xml",
    data: soapEnv,
    complete: processResult,
    contentType: "text/xml; charset='utf-8'"    
});

function processResult(xData, status) {
    $(xData.responseXML).find("PropertyData > Name:contains('FirstName')").each(function() {
        window.FirstName = $(this).parent().find("Values").text();
    });

    $(xData.responseXML).find("PropertyData > Name:contains('LastName')").each(function() {
        window.LastName = $(this).parent().find("Values").text();
    });

}

    userName += window.FirstName;
    userName += " " + window.LastName;
    console.log(userName);
Community
  • 1
  • 1
Aaron
  • 550
  • 2
  • 6
  • 23
  • 3
    Welcome to the wonderful world of **async**! You can't do that. – SLaks Nov 09 '12 at 18:56
  • You are making an async called, and assigning the window.FirstName and window.LastName to a variable *before* they are defined. – u.k Nov 09 '12 at 18:57
  • oh, and _please_ refactor those calls to `$(xData.responseXML).find("PropertyData > Name ...")` – Alnitak Nov 09 '12 at 19:27

2 Answers2

0

You're attempting to access window.FirstName and window.LastName before the AJAX call has completed. You need to put that inside your callback function:

function processResult(xData, status) {
    $(xData.responseXML).find("PropertyData > Name:contains('FirstName')").each(function() {
        window.FirstName = $(this).parent().find("Values").text();
    });

    $(xData.responseXML).find("PropertyData > Name:contains('LastName')").each(function() {
        window.LastName = $(this).parent().find("Values").text();
    });

    userName += window.FirstName;
    userName += " " + window.LastName;
    console.log(userName);

}
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
0

Async call changes the sequence of execution. processResult will be called after window.FirstName; and window.LastName; is assigned to userName, I tried to depict the sequence by calling alerts which would show the window.FirstName is available if defined before processResult body executes.

window.FirstName = "First Name: Assigned before ajax call";
window.LastName = "Last Name: Assigned before ajax call";

function processResult(xData, status) {
    $(xData.responseXML).find("PropertyData > Name:contains('FirstName')").each(function() {
        window.FirstName = $(this).parent().find("Values").text();
    });

    $(xData.responseXML).find("PropertyData > Name:contains('LastName')").each(function() {
        window.LastName = $(this).parent().find("Values").text();
    });

   userName += window.FirstName;
   userName += " " + window.LastName;
   alert("After ajax response UserName: " + userName);    
}

userName += window.FirstName;
userName += " " + window.LastName;
alert("Before ajax response UserName: " + userName);
Adil
  • 146,340
  • 25
  • 209
  • 204
  • Thanks, that makes more sense about why defining it as a window variable wasn't working. My issue is then: how do I make userName accessible elsewhere (i.e. to other functions)? – Aaron Nov 09 '12 at 19:14
  • You can call a function from processResult and pass username and do your processing there, got it? – Adil Nov 09 '12 at 19:16