我开发了一个基本的 CXF JAX-WS Web 服务。web服务、接口的一些细节:
import javax.jws.WebService;
@WebService(name = "MesseSEI", targetNamespace = "http://default_package/")
public interface MesseSEI {
public String ResponseMesse(String input);
}
服务实现:
import javax.jws.WebService;
@WebService(targetNamespace = "http://default_package/", endpointInterface = "MesseSEI", portName = "MessePort", serviceName = "MesseService")
public class Messe implements MesseSEI {
public String ResponseMesse(String input){
return input + " is a good input!";
}
}
为了在 HTML 页面中调用 Web 服务,我使用 Jquery。
Jquery部分是这样的:
var URL = 'http://localhost:8080/Messe/services/MessePort?wsdl';
var soapMessage='<?xml version="1.0" encoding="utf-8"?>';
soapMessage +='<soapenv:Envelope xmlns:q0="http://default_package/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org /2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
soapMessage +='<soapenv:Body>';
soapMessage +='<q0:ResponseMesse>';
soapMessage +='<arg0>Dolar</arg0>';
soapMessage +='</q0:ResponseMesse>';
soapMessage +='</soapenv:Body>';
soapMessage +='</soapenv:Envelope>';
function CallService()
{
$.ajax({
url: URL,
type: "POST",
dataType: "xml",
data: soapMessage,
dataProcess: false,
cache: false,
success: function(data) {
alert("Success: " + data);
},
complete: function(msg){
alert(msg.status +" " + msg.statusText + " " +msg.readyState + " " + msg.redirect);
}
});
return false;
}
首先在 Ajax 调用时使用:
contentType: "application/xml; charset=utf-8",
在服务器端请求类型显示为 OPTIONS 而不是 POST。通过从 ajax 调用中删除 contentType,Web 服务可以正常接收请求。它只是给出状态为 200 OK 的响应,并且 SoapEnv 已正确发送。但是,在 jquery 部分无法接收响应,也就是说调用了错误回调函数,在其中,我只看到消息的状态为 0。
在搜索类似问题时,我遇到可能与 CORS 问题有关,但我添加了:
jQuery.support.cors = true;
在我的代码中。
要清楚我现在的问题是,Web 服务接收请求并使用正确的 SOAPenv 发送回响应,但我猜在客户端部分(即 ajax jquery 调用)响应从未收到,所以它只是将状态设为 0,而 statusText如未定义。
我希望我清楚地提到了我的问题,任何建议表示赞赏。提前致谢。
编辑:以下是 Web 服务中显示的入站和出站消息:
入站消息
ID: 16
Address: http://localhost:8080/Deneme/services/DeepThoughtPort?wsdl
Encoding: UTF-8
Http-Method: POST
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Headers: {Accept=[application/xml, text/xml, */*; q=0.01], accept-charset=[ISO-8859-1,utf-8;q=0.7,*;q=0.7], accept-encoding=[gzip, deflate], accept-language=[en-us,en;q=0.5], cache-control=[no-cache], connection=[keep-alive], Content-Length=[299], content-type=[application/x-www-form-urlencoded; charset=UTF-8], host=[localhost:8080], origin=[null], pragma=[no-cache], user-agent=[Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1]}
Payload: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.skispike.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><q0:messe><arg0>Dolar</arg0></q0:messe></soapenv:Body></soapenv:Envelope>
出站消息
ID: 16
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:messeResponse xmlns:ns2="http://ws.skispike.com/"><return>Dolar is a good input!</return></ns2:messeResponse></soap:Body></soap:Envelope>