如何从html页面[javascript]调用webservice方法而不刷新页面

ocp*_*ocp 11 html javascript jquery web-services

我有一个webservice,它将返回一个值.我的要求是,我需要从index.html页面调用该webserive,该页面有一个html提交按钮.在该按钮上单击我正在调用javascript.从那里我想调用web方法.我怎样才能做到这一点.

我的web服务是"localhost/ws/service.asmx"; 和webmethod是HelloWorld

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

 function fun() {


    // here  I want to call the "helloworld" method.
    return true;
}
Run Code Online (Sandbox Code Playgroud)

Adi*_*tya 14

使用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;
}
Run Code Online (Sandbox Code Playgroud)

要么 :

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;
}
Run Code Online (Sandbox Code Playgroud)


Div*_*ian 7

您可以将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);
            }
        });
Run Code Online (Sandbox Code Playgroud)