madas
madas

Reputation: 73

calling webservice using jquery

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

Upvotes: 0

Views: 5526

Answers (3)

Paul Vargas
Paul Vargas

Reputation: 42020

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)

Upvotes: 1

madas
madas

Reputation: 73

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
}
}

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

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.

Upvotes: 1

Related Questions