Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a requirement to access Soap service through JavaScript


<html>
<head>
    <title>SOAP JavaScript Client Test</title>
    <script type="text/javascript">
        function soap() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open('POST', 'http://127.0.0.1:9876/ts', true);

            // build SOAP request
           var sr= '<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:ts="http://ts.ch01/">'+
  ' <x:Header/>'+
  ' <x:Body>'+
       '<ts:getAsTimeElapsed/>'+
    '</x:Body>'+
'</x:Envelope>'
alert(sr);
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {
                        alert(xmlhttp.responseText);
                        // alert('done. use firebug/console to see network response');
                    }
                }
            }
            // Send the POST request
            xmlhttp.setRequestHeader('Content-Type', 'text/xml');
            //xmlhttp.setRequestHeader('SOAPAction', 'http://127.0.0.1:9876/ts/action');
            xmlhttp.send(sr);
            // send request
            // ...
        }
    </script>
</head>
<body>
    <form name="Demo" action="" method="post">
        <div>
            <input type="button" value="Soap" onclick="soap();" />
        </div>
    </form>
</body>
</html>



while I am trying above code getting below warning message/error and not getting any response.

in back end (java) com.sun.xml.internal.ws.transport.http.server.WSHttpHandler handleExchange WARNING: Cannot handle HTTP method: OPTIONS

in front end -- Failed to load resource: net::ERR_EMPTY_RESPONSE

Could you please help me for this. any suggestions please.


What I have tried:

<html>
<head>
    <title>SOAP JavaScript Client Test</title>
    <script type="text/javascript">
        function soap() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open('POST', 'http://127.0.0.1:9876/ts', true);

            // build SOAP request
           var sr= '<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:ts="http://ts.ch01/">'+
  ' <x:Header/>'+
  ' <x:Body>'+
       '<ts:getAsTimeElapsed/>'+
    '</x:Body>'+
'</x:Envelope>'
alert(sr);
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {
                        alert(xmlhttp.responseText);
                        // alert('done. use firebug/console to see network response');
                    }
                }
            }
            // Send the POST request
            xmlhttp.setRequestHeader('Content-Type', 'text/xml');
            //xmlhttp.setRequestHeader('SOAPAction', 'http://127.0.0.1:9876/ts/action');
            xmlhttp.send(sr);
            // send request
            // ...
        }
    </script>
</head>
<body>
    <form name="Demo" action="" method="post">
        <div>
            <input type="button" value="Soap" onclick="soap();" />
        </div>
    </form>
</body>
</html>





Note: this above code works in IE and not in Edge browser.
please help me out anyone ?
Posted
Updated 24-Jul-22 22:29pm

1 solution

You're making a cross-origin request. The server needs to support CORS for that to work, which means it needs to support the OPTIONS method.

If it doesn't support that HTTP method, then it doesn't support cross-origin requests.

Cross-Origin Resource Sharing (CORS) - HTTP | MDN[^]

NB: The code works in Internet Explorer because that ancient (and now deceased[^]) browser pre-dates the CORS security specification.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900