WebService學習筆記-Ajax請求Webservice

來源:互聯網
上載者:User

標籤:ajax

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+, SafarixmlHttp = new XMLHttpRequest();} catch (e) {// Internet Explorertry {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

WebService學習筆記-Ajax請求Webservice

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.