views:

549

answers:

2

So I am trying to make a request and pass in a SOAP object, the problem is that it comes back fine (onSuccess) but the responseXML is not there. I used Fiddler and HTTP Client (OSX version of fiddler) and input the same requests, boom, both back with the correct response. With prototype, not so much.

I tried in the header and in the parameters fields...

<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript">
    function test(){
        var body = '<?xml version="1.0" encoding="utf-8"?>' +
        '<soap:Envelope' +
        ' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"' +
        ' xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"' +
        ' xmlns:tns="urn:uvindexalert" xmlns:types="urn:uvindexalert/encodedTypes"' +
        ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
        ' xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;' +
        '  <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;' +
        '    <tns:getUVIndexAlertByZipCode>' +
        '       <in0 xsi:type="xsd:string">92109</in0>' +
        '   </tns:getUVIndexAlertByZipCode>' +
        '  </soap:Body>' +
        '</soap:Envelope>';

        var headers = ["SOAPAction", " ", "Content-Type", "text/xml"];

        var request = new Ajax.Request("http://iaspub.epa.gov/uvindexalert/services/UVIndexAlertPort?wsdl", {
            contentType: "application/xml",
            requestHeaders: headers,
            parameters: "SOAPAction: ",
            postBody: body,
            onSuccess: function(response){
                var j = 0;
            },
            onFailure: function(){
                var i = 0;
            }
        });
    }

    test();
</script>

The var i & j are just so I know where it breaks so I can inspect it. I also looked online and noticed someone said try instead of text/xml application/xml then add charset=utf-8 so I did all that but still nothing. Anyone know for sure how to get back XML with a SOAPAction?

+1  A: 

You can't make cross-domain requests with Ajax. It's a security restriction.

There are a couple different efforts underway to enable safe cross-domain requests, but they aren't supported by all browsers, and they require cooperation from the site you're trying to make requests to. Many people instead use an HTTP proxy script — a script on your server that receives Ajax requests, contacts remote URLs, and passes along the response.

savetheclocktower
+1  A: 

Cross site scripting security issues aside, I've written a Prototype.js SOAP client before and had lots of problems with the 'Content-Type' and 'SOAPAction' headers. I eventually used the following and it worked (But only for requests in the same domain).

Cotent-Type: text/xml; charset=utf-8
SOAPAction: ""
Phil Peace