Ajax,Jquery请求WebService

首先启动一个WebService服务,代码如下:

SEI接口:

import javax.jws.WebMethod;
import javax.jws.WebService;

/*
 * SEI接口
 * */
@WebService
public interface HelloWS {
    @WebMethod
    public String sayHello(String name);
}

SEI实现

import javax.jws.WebService;

/*
 * SEI实现
 * */
@WebService
public class HelloWSImpl implements HelloWS {

    @Override
    public String sayHello(String name) {
        System.out.println("server sayHello()"+name);
        return "Hello"+name;
    }

}

WebService实现:

package com.lin.ws.server;

import java.util.List;

import javax.xml.ws.Endpoint;

import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws22.EndpointImpl;
import org.apache.cxf.message.Message;

import com.lin.ws.HelloWSImpl;

/*
 * 实现Web Service
 * */
public class ServerTest4 {

    public static void main(String[] args) {
        String address="http://172.19.183.17:8888/WS/hellows";
        Endpoint endpoint= Endpoint.publish(address, new HelloWSImpl());
        System.out.println(endpoint);
        EndpointImpl endpointImpl=(EndpointImpl) endpoint;

        //服务器端的日志入拦截器
        List<Interceptor<? extends Message>> inInterceptors= endpointImpl.getInInterceptors();
        inInterceptors.add(new LoggingInInterceptor());

        //服务器端的日志出拦截器
        List<Interceptor<? extends Message>> outInterceptors= endpointImpl.getOutInterceptors();
        outInterceptors.add(new LoggingOutInterceptor());


        System.out.println("发布Web Service成功!");
    }
}

启动后,创建一个html文件,准备通过ajax请求WebService服务,直接上代码,代码中有说明:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
    //服务地址
    var url="http://172.19.183.17:8888/WS/hellows";
    //JQuery请求
    $(function(){
        $("#btn").click(function(){
            var name=document.getElementById("name").value;
            //请求体
            var data='<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://ws.lin.com/"><arg0>'+name+'</arg0></ns2:sayHello></soap:Body></soap:Envelope>';
            $.post(url, data, function(msg){
                var $Result = $(msg);
                var value = $Result.find("return").text();
                alert(value);
            },"xml");
        });
    });
    //Ajax请求
    function reqWebService(){

        var name=document.getElementById("name").value;
        //请求体
        var data='<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://ws.lin.com/"><arg0>'+name+'</arg0></ns2:sayHello></soap:Body></soap:Envelope>';
        var request=getRequest();
        alert(request.status);

        request.onreadystatechange =function(){
            if(request.readyState==4&&request.status==200){
                var result=request.responseXML;
                alert(result);
                var returnElement = result.getElementsByTagName("return")[0];
                var value = returnElement.firstChild.data;
                //alert(value);
            }

        };
        //打开连接
        request.open("POST", url);
        //重新设置请求头
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        //发送请求
        request.send(data);
    }

    /* ActiveXObject有浏览器兼容问题 */
    function getRequest() {
        var xmlhttp=null;
        try{
            xmlhttp=new XMLHttpRequest();
        }catch (e) {
            try {
                 xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlhttp;
    }

</script>
</head>
<body>
    用户名:<input id="name" name="username" value=""><br>

    <button onclick="reqWebService()">Ajax请求WebService</button>
    <button id="btn">Jquery请求WebService</button>

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Radom7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值