Ajax,Jquery请求WebService
来源:互联网 发布:视频剪辑软件哪个好 编辑:程序博客网 时间:2024/05/21 06:39
首先启动一个WebService服务,代码如下:
SEI接口:
import javax.jws.WebMethod;import javax.jws.WebService;/* * SEI接口 * */@WebServicepublic interface HelloWS { @WebMethod public String sayHello(String name);}
SEI实现
import javax.jws.WebService;/* * SEI实现 * */@WebServicepublic 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>
0 0
- Ajax,Jquery请求WebService
- Web Service (013---webservice使用Jquery、Ajax请求WebService)
- jquery ajax跨域请求webservice webconfig配置
- jquery ajax跨域请求webservice webconfig配置
- jquery ajax跨域请求webservice webconfig配置
- ajax请求webservice
- Web Service (014---webservice使用Jquery、Ajax请求WebService所遇到的跨域问题)
- ajax jquery webservice
- jquery ajax webservice ???
- jquery ajax webservice
- Jquery Ajax WebService
- Jquery Ajax WebService
- jquery ajax 调用WebService
- Jquery Ajax WebService
- ajax、jquery调用webservice
- 解决webservice用Ajax(jquery)传递客户端请求时ip跨域问题(异步域请求)
- 使用Jquery Ajax的webservice请求来实现更简练的Ajax
- 使用Jquery Ajax的webservice请求来实现更简练的Ajax
- 1048. 数字加密(20)
- 数组、字符串及字符串相关函数
- IDEA基础
- 这个英语哟
- CCF NOI1075 F函数
- Ajax,Jquery请求WebService
- JavaFx 3D文件导入
- 4.剑指offer_用两个栈模拟一个队列
- 画圆
- B1011. A+B和C (15')
- 拓扑排序的简单实现
- AngularJS指令
- 链表1:从尾到头打印链表
- 《深入理解Java虚拟机》学习笔记