首页  ·  知识 ·  编程语言
webservice如何允许远程访问如果使用javascript调用webserv
网友  收集  .NET  编辑:德仔   图片来源:网络
1、 在lt;system.webgt;lt;/system.webgt;之间加下面一段 lt;webServicesgt;/p

1、 在<system.web></system.web>之间加下面一段

<webServices>

                <protocols>

                     <add name="HttpSoap"/>

                     <add name="HttpPost"/>

                     <add name="HttpGet"/>

                     <add name="Documentation"/>

                </protocols>

           </webServices>

因为我的webservice是使用javascript 在页面调用,而且采用post方式通过httprequest调用webservice,所以

其实只需要<add name="HttpPost"/> 这个配置

2、

      使用javascript 调用 webservice

   var PNRList=new Array(<%=PNRArray %>);---c#代码中定义的string数组
   var currentIndex=-1;
   function StartCheckPNRStatus() --
   {
        if(PNRList.length!=0)
        {
           setTimeout(checkStatus,2000);--页面加载完毕后,2秒调用方法

      //setInterval(checkStatus,2000);使用定时器
        }
   }

 

 function checkStatus()
    {
        currentIndex++;
        if(currentIndex==PNRList.length)
        {
            //clearInterval(timerHandle) 定时器,在前面定义个timerhandle

        }
        else
        {
            var httpRequest;
            if(window.XMLHttpRequest)
            {
                httpRequest=new window.XMLHttpRequest();
            }
            else
            {
                httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
            }
            httpRequest.onreadystatechange=function ()
            { 

                if( httpRequest.readyState==4 )
                {
                    if( httpRequest.status==200)
                    {
                        var xmlDom=httpRequest.responseXML.documentElement;
                        var PNRStatus=xmlDom.childNodes(0).text;--由于我返回的是字符串,所以可以这样取值
                        //alert(PNRStatus)
                        spanPNRStatus.innerText=PNRStatus;
                        if(PNRStatus!="RR")
                        {
                           /* spanLogBtn.style.display="";
                            spanCheckBtn.style.display="";
                            noRRCount++;
                            var spanNumOfTicket=document.getElementById("spanNumOfTicket"+PNRList[currentIndex]);//失败订单票数
                            var spanPriceDue=document.getElementById("spanPriceDue"+PNRList[currentIndex]);//失败订单金额
                            totalTicketNum+=parseInt(spanNumOfTicket.innerText);
                            totalPriceDue+=Number(spanPriceDue.innerText);
                            //给数据统计头赋值
                            var spanTotalOrderCount=document.getElementById("spanTotalOrderCount");
                            var spanRRCount=document.getElementById("spanRRCount");
                            var spanNoRRCount=document.getElementById("spanNoRRCount");
                            var spanTotalTicketNum=document.getElementById("spanTotalTicketNum");
                            var spanTotalPriceDue=document.getElementById("spanTotalPriceDue");
                           
                            spanTotalOrderCount.innerText=PNRList.length;
                            spanRRCount.innerText=PNRList.length-noRRCount;
                            spanNoRRCount.innerText=noRRCount;
                            spanTotalTicketNum.innerText=totalTicketNum;
                            spanTotalPriceDue.innerText=totalPriceDue.toFixed(2);  */ 这块是自定义
                          }
                            checkStatus();//当返回结果时,继续下一次取数组的值,继续调用webservice
                       }
                  }    
             }//end httpRequest.onreadystatechange
            httpRequest.open("post", "IBEServiceGetPnrStatusByPNR.asmx/GetPNRStatusFromPNR", true);
            httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            httpRequest.send("PNR="+PNRList[currentIndex]);
          
        }  
    }

   StartCheckPNRStatus();

</script>--在页面加载完毕后  执行这个调用webservice的方法

也可以使用定时器来定时调用webservice

本文作者:网友 来源:网络收集
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读