Using the XmlHttpRequest object with .NET Web Services

var req;

if(window.XMLHttpRequest) {
      try {
            req = new XMLHttpRequest();
      } // try
      catch(e) {
            req = false;
      } // catch
} // if
else {
      if(window.ActiveXObject) {
            try {
                  req = new ActiveXObject("Msxml2.XMLHTTP");
            } //try
            catch(e) {
                  try {
                        req = new ActiveXObject("Microsoft.XMLHTTP");
                  } // try
                  catch(e) {
                        req = false;
                  } // catch
            } // catch
      } // if
} //else

if(req) {
      req.onreadystatechange = eventHandler;
      req.open("POST", url, true);
      req.setRequestHeader("Man", "POST url HTTP/1.1");
      req.setRequestHeader("Host", "address");
      req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
      req.setRequestHeader("Content-Length", post.length);
      req.setRequestHeader("SOAPAction", "soapaction");
      req.send(post);
} // if

The url, address, and soapaction placeholders are filled in with their proper values.  The post variable is well formed XML, identical to that provided by the .NET sample SOAP request, with the placeholders filled in with their proper values.  I get the following server side error:

System.InvalidOperationException: Request format is invalid: text/xml; charset=utf-8.
      at System.Web.Services.Protocols.HttpServerProtcol.ReadParameters()
      at System.Web.Services.Protocols.WebServiceHandler.Invoke()
      at System.Web.Services.Protocols.CoreProcessRequest()

I cannot figure out how to correctly invoke the webservice using SOAP.  HTTP POST cannot be used because the services require complex data. However in my attempt to get it to work, I modified the service to require two simple strings.  The HTTP POST does successfully return the data I want, but it will not suffice in the future, which is why I need to get the SOAP to work.  Here is the XML I am putting in the post variable.

var post = "<?xml version=\"1.0\" ?>" +
      "<soap:Envlope 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/\">" +
      "<soap:Body>" +
      "<ReadData xmlns=\"http://webservices.jirasoft.com/WebServicesCore/Data\">" +
      "<id>" + id + "</id>" +
      "<name>" + name + "</name>" +
      "</ReadData>" +
      "</soap:Body>" +
      "</soap:Envelope>";

Thanks in advance for your assistance!
brian6504Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

amit_gCommented:
Change Content-Type header to...

req.setRequestHeader("Content-Type", "text/xml");
brian6504Author Commented:
The error is the same, except without the charset=utf8
System.InvalidOperationException: Request format is invalid: text/xml.:
     at System.Web.Services.Protocols.HttpServerProtcol.ReadParameters()
     at System.Web.Services.Protocols.WebServiceHandler.Invoke()
     at System.Web.Services.Protocols.CoreProcessRequest()

And, for what it's worth, when the Content-Type header is omitted, the exception becomes:
System.InvalidOperationException: Request format is invalid: .
     at System.Web.Services.Protocols.HttpServerProtcol.ReadParameters()
     at System.Web.Services.Protocols.WebServiceHandler.Invoke()
     at System.Web.Services.Protocols.CoreProcessRequest()
brian6504Author Commented:
I have just learned that if I invoke a parameterless web service, it works just fine, but when I need to include arguments, the framework throws the exception.
amit_gCommented:
Most likely your URL is incorrect. Make sure that the URL doens't contain the method name. For example you are probably using

http://YourServer/YourWebApplication/YourWebService.asmx/YourMethod

change it to

http://YourServer/YourWebApplication/YourWebService.asmx

The reason is that in SOAP SOAPAction determines which method is going to be called. When you give method name in URL that becomes a GET/POST and that is why the ASP.NET application is throwing error.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.