WebService学习笔记-Ajax请求Webservice

Webservice地址为 http://192.168.13.232:8989/ws_01/umgsai

JSP页面地址为 http://192.168.13.232:8080/Demo/index.jsp

Webservice的请求体如下

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.umgsai.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <q0:sayHello>
      <arg0>sa</arg0>
    </q0:sayHello>
  </soapenv:Body>
</soapenv:Envelope>

Webservice的响应体如下

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
    <ns2:sayHelloResponse xmlns:ns2="http://ws.umgsai.com/">
      <return>Hello sa</return>
    </ns2:sayHelloResponse>
  </S:Body>
</S:Envelope>

JSP页面如下

<%@ 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="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
	var url = "http://192.168.13.232:8989/ws_01/umgsai";
	$(function(){
		$("#WebserviceJQuery").click(function(){
			var name = document.getElementById("username").value;
			//请求体
			var data = ‘<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.umgsai.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><q0:sayHello><arg0>‘ + name + ‘</arg0></q0:sayHello></soapenv:Body></soapenv:Envelope>‘;
			/*
			$.post(url, data, function(msg){
				var $Result = $(msg);
				var value = $Result.find("return").text();
				alert(value);
			},"xml");
			*/
			$.ajax({
				type:"POST",
				url:url,
				data:data,
				success:function(msg){
					var $Result = $(msg);
					var value = $Result.find("return").text();
					alert(value);
				},
				error:function(msg){
					alert("错误信息:" + msg);
				},
				datatype:"xml",
				contentType:"text/xml;charset=utf-8"
			});
		});
	});

	function reqWebservice() {
		var name = document.getElementById("username").value;
		//请求体
		var data = ‘<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.umgsai.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><q0:sayHello><arg0>‘ + name + ‘</arg0></q0:sayHello></soapenv:Body></soapenv:Envelope>‘;
		var request = GetXmlHttpObject();
		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);
			}
		};
		//响应体 <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
/*
		<S:Body>
		    <ns2:sayHelloResponse xmlns:ns2="http://ws.umgsai.com/">
		      <return>Hello sa</return>
		    </ns2:sayHelloResponse>
		  </S:Body>
		</S:Envelope>

*/
		request.open("POST", url);
		request.setRequestHeader("Content-type", "text/xml;charset=utf-8");
		request.send(data);
	}

	function GetXmlHttpObject() {
		var xmlHttp = null;
		try {
			// Firefox, Opera 8.0+, Safari
			xmlHttp = new XMLHttpRequest();
		} catch (e) {
			// Internet Explorer
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		return xmlHttp;
	}
</script>
</head>
<body>
	name:
	<input id="username" name="username" value="" />
	<button onClick="reqWebservice()">Ajax请求Webservice</button>
	<button id="WebserviceJQuery">Ajax请求Webservice with JQuery</button>
</body>
</html>

存在跨域访问的问题,只能通过 http://192.168.13.232:8080/Demo/index.jsp 来访问。

通过 http://localhost:8080/Demo/index.jsp来访问是提示错误。

时间: 2024-08-08 09:25:17

WebService学习笔记-Ajax请求Webservice的相关文章

WebService学习笔记-Ajax通过servlet请求跨域Webservice

ajax不能直接访问跨域的Webservice,但是可以通过servlet去访问跨域的Webservice 页面 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

WebService学习笔记系列(二)

soap(简单对象访问协议),它是在http基础之上传递xml格式数据的协议.soap协议分为两个版本,soap1.1和soap1.2. 在学习webservice时我们有一个必备工具叫做tcpmon,该工具可以直接下载得到.使用tcpmon可以嗅探网络中传输的数据,便于我们更好的理解soap协议. 下载好tcpmon之后,打开该软件,如图简单设置 tcpmon相当于一个代理服务器,打开tcpmon后,如果把监听端口设置为9999,目标端口设置为8888,当用户访问9999端口时,消息会被tcp

WebService学习笔记-XML&Schema&HTTP

XML约束 在XML技术里,可以编写一个文档(文件)来约束一个XML文档的书写规范,这称之为XML约束 1.namespace 相当于schema文件的id 2.targetNamespace属性 用来指定schema文件的namespace的值 3.xmlns属性 引入一个约束, 它的值是一个schema文件的namespace值 4.schemaLocation属性 用来指定引入的schema文件的位置 schema规范中: 1. 所有标签和属性都需要有schema文件来定义 2. 所有的s

WebService学习笔记汇总

鲁春利的工作笔记,谁说程序员不能有文艺范? WebService学习笔记(一)基础入门 http://luchunli.blog.51cto.com/2368057/1722944 WebService学习笔记(二)WDSL格式 http://luchunli.blog.51cto.com/2368057/1724887

[ajax 学习笔记] ajax 的服务器响应

在上一篇[ajax 学习笔记] ajax初试中,简单了解了一下ajax. 我是参考AJAX详解.chm学习的,资源已上传.参考链接:Ajax 专题 今天又学习了ajax中关于服务器响应的一些知识. ajax中服务器的响应是通过响应函数将服务器返回的数据呈现到页面的.服务器的响应体现在服务器响应回调函数中. 在上一篇的例子中,响应函数为: function updatePage(){ if(xmlHttp.readyState == 4){ //http就绪状态 if(xmlHttp.status

iOS学习笔记---网络请求

一.HTTP协议的概念 HTTP协议:Hyper Text Transfer Protocol(超文本传输协议)是用于从万维网服务器传送超文本到本地浏览器的传输协议.HTTP是一个应用层协议,由请求和响应构成,是一个标准的客户端服务器模型(c/s) HTTP协议的特点 支持客户/服务器模式(c/s模式) 简单快速 灵活,HTTP允许任意类型的对象进行传输,正在传输的对象类型由Content-type加以标记 iOS学习笔记---网络请求

webService学习笔记

xsd ---->定义了schema标签的namespacewsdl ---->定义了wsdl标签的namespace <definitions> <types> <schema> <element> </schema> </types> <message> </message> <portType> </portType> <binding> </bind

WebService学习笔记-CXF添加拦截器

Webservice拦截器:在webservice请求过程中,动态操作请求和响应的数据 分类 按照所处的位置分:服务器端拦截器  客户端拦截器 按照消息的方向分:入拦截器  出拦截器 按照定义者分:系统拦截器 自定义拦截器 在服务器端添加拦截器 package com.demo; //注意引入的类一定要正确 import javax.xml.ws.Endpoint; import org.apache.cxf.interceptor.LoggingInInterceptor; import or

原生js学习笔记——Ajax基础

1.什么是Ajax Ajax:是Asynchronous Javascript And XML的简写,即异步JavaScript和XML XMLHttpRequest 提供的异步,是指基于 ajax 的应用在服务器中的通信方式. 传统的 web 应用,每次用户向服务器请求获得新数据时,浏览器都会丢弃当前页面,而等待重新加载新的页面.这样一来,在服务器完全响应之前,用户浏览器将一片空白,用户的动作必须中断.而异步指用户发送请求后,完全无需等待,请求会在后台发送,不会阻塞用户当前活动.这样一来,用户