title: Calling a Soap Web Service function from Javascript using XMLHttpRequest
body: <p>Parameters of the function in the web service:  </p>
  
  <pre><code>POST /_vti_bin/******.ConnectorService.asmx HTTP/1.1
  Host: www.*****.com
  Content-Type: text/xml; charset=utf-8
  Content-Length: length
  SOAPAction: "http://www.*****.com/SaveFile"
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;soap:Body&gt;
      &lt;SaveFile xmlns="http://www.proged.tn"&gt;
        &lt;urlSite&gt;string&lt;/urlSite&gt;
        &lt;iListName&gt;string&lt;/iListName&gt;
        &lt;iUpdateMode&gt;NotVerified or FillEmpty or Replace or NotReplace&lt;/iUpdateMode&gt;
        &lt;iMetaData&gt;xml&lt;/iMetaData&gt;
        &lt;iAttachments&gt;xml&lt;/iAttachments&gt;
        &lt;iOverWrite&gt;boolean&lt;/iOverWrite&gt;
      &lt;/SaveFile&gt;
    &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>My js code:  </p>
  
  <pre><code>function sendReq()
  {
  var my_title="title";
  var c='&lt;img alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhE.....AAAElFTkSuQmCC" /&gt;';
      var b= false;
      var wsUrl = "http://www.proged.tn/sites/toyourbusiness/_vti_bin/PROGED.ConnectorService.asmx?op=UpdateListItem";
      var soapRequest ='&lt;?xml version="1.0" encoding="utf-8"?&gt;'+
          '&lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;'+
    '&lt;soap:Body&gt;'+
      '&lt;SaveFile xmlns="http://www.*****.com"&gt;'+
        '&lt;urlSite&gt;http://www.*****.com/***/***&lt;/urlSite&gt;'+
        '&lt;iListName&gt;{*****}&lt;/iListName&gt;'+
        '&lt;iUpdateMode&gt;NotVerified&lt;/iUpdateMode&gt;'+
        '&lt;iMetaData&gt;&lt;Fields&gt;&lt;Field Name="Title" &gt;'+my_title+'&lt;/Field&gt;&lt;/Fields&gt;&lt;/iMetaData&gt;'+
        '&lt;iAttachments&gt;&lt;Attachments&gt;&lt;Attachment Name="name of the file"&gt;'+c+'&lt;/Attachment&gt;&lt;/Attachments&gt;&lt;/iAttachments&gt;'+
        '&lt;iOverWrite&gt;'+b+'&lt;/iOverWrite&gt;'+
      '&lt;/SaveFile &gt;'+
    '&lt;/soap:Body&gt;'+
  '&lt;/soap:Envelope&gt;';
      var xmlhttp =  createXMLHttpRequest();
  
      xmlhttp.open("POST", wsUrl, true,"username","password");
  
      xmlhttp.setRequestHeader ("Post", "/_vti_bin/****.ConnectorService.asmx HTTP/1.1");
      xmlhttp.setRequestHeader ("Host", "www.****.com");
      xmlhttp.setRequestHeader("Access-Control-Allow-Origin", "*");
      xmlhttp.setRequestHeader ("Content-Type", "text/xml; charset=utf-8");
      xmlhttp.setRequestHeader("Content-Length", soapRequest.length);
      xmlhttp.setRequestHeader ("SOAPAction", "http://www.****.com/SaveFile");
  
      xmlhttp.onerror = function(e) {
          alert("Error ocurred. Error = " + e.message);
      }
      xmlhttp.ontimeout = function(e) {
          alert("Timeout error!");
      }
      xmlhttp.onreadystatechange  = function () { 
          alert(xmlhttp.readyState);
  
          if (xmlhttp.readyState==4)
          {
              alert(xmlhttp.responseText);
              alert(xmlhttp.status);
              // if "OK"
              if (xmlhttp.status==200 || xmlhttp.status==0)
              {
                  alert(xmlhttp.responseXML);
              //[Get xmlhttp.responseXML.xml and do something with it]
  
              }
              else
              {
              //[Get xmlhttp.responseXML.xml and do something with it in the case of an error]
              }
          }
      }
  
      xmlhttp.send(soapRequest);
  }
  </code></pre>
  
  <p>This returns xmlhttp.responseXML=null, status=0, what's wrong!!</p>