call java web service from javascript
11,902
I would take SOAP UI, generate web service client, generate example requests, so I don't need to create SOAP envelopes from scratch. Then I would use jQuery to generate AJAX requests with the help of generated SOAP envelopes.
Another approach would be to make use of http://cxf.apache.org/docs/javascript-clients.html - you will have complete JavaScript generated that way.
Author by
madas
Updated on June 04, 2022Comments
-
madas almost 2 years
I would like to call my webservice methods from pure java script code. and that code should work on mozilla browser.
This is my webservice code:
package com.example.core; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebService public class Area { @WebMethod public double square(@WebParam(name="side") double side) { return side * side; } @WebMethod public double rectangle(@WebParam(name="length") double length,@WebParam(name="breadth") double breadth) { return length * breadth; } public static void main(String[] args) { Area area = new Area(); String url = "http://localhost:8090/area"; // end point of webservice. System.out.println(url+"?wsdl"); Endpoint.publish(url, area); // publishing the webservice } }
Here is my HTML file:
<html> <head> <meta content="utf-8" http-equiv="encoding"> <meta content="text/xml;charset=utf-8" http-equiv="Content-Type"> <script language="javascript"> function call() { var side = sideid.value; var side1 = sideid1.value; var req = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://core.codon.com/\"><soapenv:Body><web:rectangle><length>" + side+ "</length><breadth>" + side1+ "</breadth></web:rectangle></soapenv:Body></soapenv:Envelope>"; //var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); //var reqXML = xmlDoc.loadXML(req); var xmlDoc=document.implementation.createDocument("", "", null); xmlDoc.async=false; xmlDoc.onload = req; //var reqXML = xmlDoc.load(req); var xmlhttp; if(window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { var response = xmlhttp.responseXML; alert(response.selectSingleNode(".//return").text); alert("======"+response); } } var soapaction = "http://core.example.com/rectangle"; xmlhttp.open("POST","http://localhost:8090/area?wsdl",true); xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xmlhttp.setRequestHeader("SOAPAction", soapaction); xmlhttp.send(req); } </script> </head> <body> Side Length: <input type="text" id="sideid"></input> Length: <input type="text" id="sideid1"></input> <button onclick="call();">area of square</button> </body> </html>
with the above code am getting response as null. The same code working on IE but not in mozilla...
my webservice side am getting the following error
com.sun.xml.internal.ws.transport.http.server.WSHttpHandler handleExchange WARNING: Cannot handle HTTP method: OPTIONS
Please Help me out..Thanks in advance