WebService学习笔记-Ajax请求Webservice
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了WebService学习笔记-Ajax请求Webservice,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4136字,纯文字阅读大概需要6分钟。
内容图文
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来访问是提示错误。
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1563938
原文:http://shamrock.blog.51cto.com/2079212/1563938
内容总结
以上是互联网集市为您收集整理的WebService学习笔记-Ajax请求Webservice全部内容,希望文章能够帮你解决WebService学习笔记-Ajax请求Webservice所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。