ajax调用webservice

<html>

<head>

<title>通过ajax调用WebService服务</title>

<script>

varxhr=newActiveXObject("Microsoft.XMLHTTP");

functionsendMsg(){

varname=document.getElementById('name').value;

//服务的地址

varwsUrl='http://192.168.1.100:6789/hello';

//请求体

varsoap='<soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:q0="http://ws.itcast.cn/"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>';

//打开连接

xhr.open('POST',wsUrl,true);

//重新设置请求头

xhr.setRequestHeader("Content-Type","text/xml;charset=UTF-8");

//设置回调函数

xhr.onreadystatechange=_back;

//发送请求

xhr.send(soap);

}

function_back(){

if(xhr.readyState==4){

if(xhr.status==200){

//alert('调用Webservice成功了');

varret=xhr.responseXML;

varmsg=ret.getElementsByTagName('return')[0];

document.getElementById('showInfo').innerHTML=msg.text;

//alert(msg.text);

}

}

}

</script>

</head>

<body>

<inputtype="button"value="发送SOAP请求"onclick="sendMsg();">

<inputtype="text"id="name">

<divid="showInfo">

</div>

</body>

</html>

相关推荐