webservice 如何允許遠端存取?如果使用javascript 調用 webservice

來源:互聯網
上載者:User

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

 

  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.