異步調用ajax webservice

異步請求返回的內容格式:文本,xml,json。javascript

能夠返回簡單的對象html

異步調用:java

使用webservice的異步調用:jquery

(1)返回文本web

<html>ajax

<head>json

<title></title>app

<script src="jquery-1.4.2.min.js" type="text/javascript"></script>異步

<script type="text/javascript">ide

$(function (){

     $("#Button1").click(function(){

 $.ajax({

   contentType:"application/json",

type:"POST",

url:"webService.asmx/Select",---調用webservice裏面的select方法

data:"{name:'"+$("#Text1").val()+"'}",

dataType:"json",

success:function(result){

alert(result.d);

}//經過webservice請求數據,返回文本

});

 });

});

</script>

</head>

<body>

<form id="form1" runat="server">

<div>

<input id="Text1" type="text" /><input id="Button1" type="button" value="button" />

</div>

</form>

</body>

</html>

(2)返回一個集合

<html>

<head>

<title></title>

<script src="jquery-1.4.2.min.js" type="text/javascript"></script>

<script type="text/javascript">

$(function (){

  $("#Button1").click(function (){

  $.ajax({

  contentType:"application/json",

type:"POST",

url:"WebService.asmx/SelectList",//是方法名稱

data:"{name:'"+$("#Text1").val()+"'}",

dataType:"json",

success:function(result){

alert(result.d.join()); join中間不須要添加","號

}//經過webservice請求數據返回集合

 

下面是webservice的內容:

/// <summary>
///WebService 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要容許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的註釋。
 [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    static List<string> list=new List<string>();
    public WebService () {

        //若是使用設計的組件,請取消註釋如下行
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public string Select(string name)
    {
        return name+"已經存在";
    }
    [WebMethod]
    public List<string> SelectList(string name)
    {
        list.Add(name);
        return list;
    }
   
}

});

});

});

</script>

</head>

<body>

<div>

<input id="Text1" type="text" /><input id="Button1" type="button" value="button" />

</div>

</body>

</html>

 

直接ajax請求數據:

<html>

 

</html>

相關文章
相關標籤/搜索