title: javascript embedded function and embedded variable scope
body: <p>I am writing a small app that will use the jQuery ajax call to make SOAP calls to a third party API.  I want to make the following function "generic" but am running into variable scoping issues in the innermost function in the "done()" ajax method.  That function is where the good stuff is returned.  I want the key, value pairs to be placed in the "result" object that is passed into the APICall function but i don't know how to reference it down there.</p>
  
  <p>I don't think I can modify the function parameter list in the "done" section can I?  I just tried adding the passed in "result" to the "done" functions and there were no syntax issues but it is not working.</p>
  
  <p>Ideas?</p>
  
  <pre><code>function APICall(method, username, password, payload, result) {
      var wsUrl = "https://localhost.my.company.org/API/Service.asmx";
  
      var soapRequest = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n"
              + "&lt;soap:Envelope \n"
              + "    soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
              + "    xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" \n"
              + "    xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
              + "    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n"
              + "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"&gt; \n"
              + "  &lt;soap:Body&gt; \n"
              + "    &lt;__METHOD__ xmlns=\"http://THIRD-PARTY.API.Web.Service/\"&gt; \n"
              + "      &lt;UserName&gt;__USERNAME__&lt;/UserName&gt; \n"
              + "      &lt;Password&gt;__PASSWORD__&lt;/Password&gt; \n"
              + "      __PAYLOAD__ \n" + "    &lt;/__METHOD__&gt; \n"
              + "  &lt;/soap:Body&gt; \n" + "&lt;/soap:Envelope&gt;\n";
  
      soapRequest = soapRequest.replace(/__METHOD__/g, method);
      soapRequest = soapRequest.replace(/__USERNAME__/g, username);
      soapRequest = soapRequest.replace(/__PASSWORD__/g, password);
      soapRequest = soapRequest.replace(/__PAYLOAD__/g, payload);
  
      $.ajax({
              type : "POST",
              headers : {
                      SOAPAction : '"http://THIRD-PARTY.API.Web.Service/' + method + '"'
              },
              url : wsUrl,
              contentType : "text/xml",
              dataType : "xml",
              data : soapRequest,
              error : processAJAXError
       }).done(
              function(data, textStatus, jqXHR) {
                      $($.parseXML($(data).find(method + 'Result').text())).find(
                              'Data').each(
                              function(key, val) {
                                  result[$(val).find('Room').text()] = $(val).find(
                                          'ID').text();
                                      console.log($(val).find('Room').text() + ' =&gt; '
                                              + $(val).find('ID').text());
                              });
               });
  }
  </code></pre>