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

来源:互联网 发布:淘宝详情模板怎么做 编辑:程序博客网 时间:2024/06/13 22:32

// 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>

原创粉丝点击