2

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

5
  • 1
    Use a js framework like jquery. That will take care of most cross browser issues. Commented Apr 23, 2013 at 13:22
  • What is the xmlhttp.status? Do you see any errors? Commented Apr 23, 2013 at 13:25
  • am getting xmlhttp.status as 0 Commented Apr 23, 2013 at 13:37
  • Chnage ur check to if (xmlhttp.readyState==4 && xmlhttp.status==200) Commented Apr 23, 2013 at 13:40
  • if i write if (xmlhttp.readyState==4 && xmlhttp.status==200) ..control not coming in to the if block..because am getting status code 0 Commented Apr 23, 2013 at 13:50

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

can you give me clear steps...because am newbie to webservices...Thanks
0

You are running webservice as stand alone app on port say 'x' and the client might be on another port say 'y' When you do a post call through y onto x the method will always be changed to options automatically. Internet standards wont allow 'posting' between different servers, I guess. You will have to find another workaround.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.