您的位置:首页 > Web前端 > JavaScript

工作中用到的非常有用的JS调用WebService

2008-02-13 17:14 316 查看
// JScript 文件

function submitvote(vnum){
var xmlHttp=createXH();
xmlHttp.open("POST","http://test1.com:5750/VoteWebService.asmx/vote",false);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send("vnum="+vnum);
var message=xmlHttp.responseXML;
var result=message.childNodes[1].text;
if(result.indexOf('|')>0){
var arr = result.split('|');
var obj = document.getElementById("mm"+vnum);
obj.innerText=arr[1];
if(arr[0]=='1'){
alert('投票成功!');
}else{
alert('每人每天只能投一票!');
}
}
}

function initvotedata(vnum){
var xmlHttp=createXH();
var message;
var result;
var obj;
xmlHttp.open("POST","http://test1.com:5750/VoteWebService.asmx/showvotenum",false);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send("vnum="+vnum);
message=xmlHttp.responseXML;
result=message.childNodes[1].text;
obj = document.getElementById("mm"+vnum);
obj.innerText=result;
}

function createXH(){
var A=null;
try {
A=new ActiveXObject('Msxml2.XMLHTTP'); //1.0
}
catch(e) {
try{
A=new ActiveXObject('Microsoft.XMLHTTP');//1.1
}
catch(oc) {
A=null;
}
}
if ( !A && typeof XMLHttpRequest != 'undefined' )
{
A=new XMLHttpRequest();
}
return A;
}

此外必须在VS2005版的WEB.CONFIG中添加

<system.web>
<webServices>
<protocols>
<add name="HttpSoap" />
<add name="HttpPost" />
<add name="HttpGet" />
<add name="Documentation" />
</protocols>
</webServices>
</system.web>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: