簡體   English   中英

如何從html頁面[javascript]調用webservice方法而不刷新頁面

[英]How to call a webservice method from an html page [javascript] with out refresh the page

我有一個webservice ,它將返回一個值。 我的要求是,我需要從index.html頁面調用該webservice ,該頁面有一個html提交按鈕。 在該按鈕上單擊我正在調用JavaScript. 從那里我想調用web方法。 我該怎么做到這一點。

我的web服務是"localhost/ws/service.asmx"; 和web方法是HelloWorld

 <input type="button" value="Submit" id="btn_submit" onclick ="return fun()">

 function fun() {


    // here  I want to call the "helloworld" method.
    return true;
}

使用jQuery進行performin POST或來自html頁面的GET請求,如下所示:

function fun() 
{
   var data="hello";
   $.get("http://localhost/ws/service.asmx/HelloWord", function(response) {
        data = response;
   }).error(function(){
  alert("Sorry could not proceed");
});

   return data;
}

要么 :

function fun() 
{
  var data="hello";
  $.post('http://localhost/ws/service.asmx/HelloWord',{},function(response) 
  {     data = response;
  }).error(function(){
  alert("Sorry could not proceed");
});

    return data;
}

您可以將ajax請求發送到Web服務

 $.ajax({
            url: "WebServiceURL",
            data: "", //ur data to be sent to server
            contentType: "application/json; charset=utf-8", 
            type: "GET",
            success: function (data) {
               alert(data);
            },
            error: function (x, y, z) {
               alert(x.responseText +"  " +x.status);
            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM