0

I am trying to call jax-ws webservice from javascript using jquery. Here is my HTML code

 <html>
  <head>
    <title>Calling Web Service from jQuery</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnCallWebService").click(function (event) {


            var soapRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema/\" xmlns:core=\"http://core.codon.com/\"><soapenv:Body><core:rectangle><length>" + "12"+ "</length><breadth>" + "10"+ "</breadth></core:rectangle></soapenv:Body></soapenv:Envelope>";


            var url11 = "http://localhost:8090/area?wsdl";


        $.ajax({
                type: "POST",
                url: url11,
                contentType:  "text/xml; charset=\"utf-8\"",
                dataType: "xml",
                data: soapRequest,
         processData: false,
                success: processSuccess,
                error: processError
            });

        });
    });

    function processSuccess(data, status, req) {
        if (status == "success")
        alert(req.responseText + " @@@" + status);
            $("#response").text();
    }

    function processError(data, status, req) {
        alert(req.responseText + " " + status);
    }  

    </script>
 </head>
<body>
<h3>
    Calling Web Services with jQuery/AJAX
 </h3>

    <input id="btnCallWebService" value="Call web service" type="button" />
   <div id="response" />
 </body>
 </html>

This code working from IE and am getting response alert as xml format. But its not working on mozilla..whats wrong with this code. can you please help me out. Thanks in advance

madas
  • 73
  • 1
  • 5
  • 15

3 Answers3

1

Make sure the web service is returning you the xml with the output header generation/ContentType set to text/xml. Although IE don't care but FF and Chrome did.

Setting the Content-type header should solved it.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • Already i have content type in my code.... contentType: "text/xml; charset=\"utf-8\"", – madas May 06 '13 at 13:42
  • You have it in the request you are sending but you need it in the response you are getting from the server. – Juned Ahsan May 06 '13 at 13:55
  • am calling jsx-ws webservice ... from there how can i add it to response? here am giving you my webservice code – madas May 06 '13 at 14:17
1

If you are executing the AJAX call from, e.g. a local file, so you have a file:///C:/Users/Paul/Desktop/index.html, aplies the Same origin policy, then you are doing a cross domain call.

In that case, the navigator sends an OPTIONS http request to the server for asking if this allow the call, but this response with Cannot handle HTTP method: OPTIONS.

The Cross-Origin Resource Sharing standard works by adding new HTTP headers that allow servers to describe the set of origins that are permitted to read that information using a web browser. Additionally, for HTTP request methods that can cause side-effects on user data (in particular, for HTTP methods other than GET, or for POST usage with certain MIME types), the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with an HTTP OPTIONS request header, and then, upon "approval" from the server, sending the actual request with the actual HTTP request method. Servers can also notify clients whether "credentials" (including Cookies and HTTP Authentication data) should be sent with requests.

HTTP access control (CORS)

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • yes am executing the AJAX call from local file system.. what changes i need to do in my javascript? – madas May 07 '13 at 09:35
0
package com.codon.core;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.Endpoint;
import javax.xml.ws.ResponseWrapper;

@WebService
//@BindingType(JSONBindingID.JSON_BINDING)
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)
{
System.out.println("==="+length+"==="+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
}
}
madas
  • 73
  • 1
  • 5
  • 15
  • Check this link, it may help:http://stackoverflow.com/questions/4648647/jax-ws-with-com-sun-net-httpserver-and-net – Juned Ahsan May 06 '13 at 16:19
  • i tried with the following annotations. @WebService @BindingType(javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING) eventhough am not able to call my webservice from firefox... – madas May 07 '13 at 09:47