abs
abs

Reputation: 650

Why is the request body empty when trying to call SOAP webservice with jQuery AJAX?

I have constructed a full soap envelope soapEnvelopeXML and I can see the call come through my TCP/IP Monitor, but the issue is that instead of calling the right method, the request URI comes in concatenated with the soapEnvelopeXML as if it is an option in the header. So i am not getting any result. I don't want to pass in the parameters as JSON objects. Any ideas on where i could be going wrong?

$.ajax({
    type: "GET",
    url: "http://localhost:8080/webservice/serviceName/",
    data: soapEnvelopeXML,
    contentType: "text/xml",
    dataType: "xml",       
    error: function(xhr, status, error) { 
        alert("Error processing your request: \n" + status + " : " + error);
    },
    success: function(response){
        var xml = $(response);
        alert(xml);
    }
});

I have tested the code on top to use POST instead of GET but I was getting an empty response and I see that an empty webservice call was being made. I didnt know that GET was illegal for SOAP requests. Does anyone know why the sent content is an empty call?

Upvotes: 1

Views: 4732

Answers (3)

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59151

Maybe the problem is that SOAP should be POST?

http://www.coderanch.com/t/463869/Web-Services/java/SOAP-request-as-HTTP

Q: How do I send a SOAP request as HTTP GET?

A: You can't. SOAP always uses POST. Only REST uses GET (as well as various other HTTP methods, including POST).

Update:

Thanks for this, I was using GET because an empty web-service call was was made when I use POST. So any ideas as to why I make empty web-service calls when I use POST?

I am getting the same behavior on my local box if I go to localhost, but I got it working by doing this:

  1. Make up a domain name
  2. Stick it in the hosts file, pointing to 127.0.0.1
  3. Access the local webserver with that fake domain

Try this code, it is working for me:

var soapEnvelopeXML= "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"> <soap:Header>CUSTOM HEADER</soap:Header> <soap:Body>PARAMS</soap:Body> </soap:Envelope>";
$.ajax({
    type: "post",
    url: "http://superblah.com/webservice/serviceName/",
    contentType: "text/xml; charset=\"utf-8\"",
    dataType: "xml",       
    data: soapEnvelopeXML,
    processData: false,
    beforeSend: function(xhr){
      xhr.setRequestHeader(
        "SOAPTarget",
        "http://superblah.com/webservice/serviceName/"
      );
      xhr.setRequestHeader(
        "SOAPAction",
        "http://superblah.com/webservice/serviceName/Something"
      );
    },
    error: function(xhr, status, error) { 
        alert("Error processing your request: \n" + status + " : " + error);
    },
    success: function(response){
        var xml = $(response);
        alert(xml);
    }
});

I got this code from here:

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100205

$.ajax({
    type: "POST",
    url: "http://localhost:8080/webservice/serviceName/",
    data: soapEnvelopeXML,
    contentType: "text/xml",
    dataType: ($.browser.msie) ? "text" : "xml",       
    error: function(xhr, status, error) { 
        alert("Error processing your request: \n" + status + " : " + error);
    },
    success: function(response){
        var xml = $(response);
        alert(xml);
    }
});

Upvotes: 0

tobyodavies
tobyodavies

Reputation: 28109

The reason it is concatenated with the URL is that is what the GET method means. Change this to POST and I suspect it will work for you.

Upvotes: 0

Related Questions