`
backspace
  • 浏览: 132787 次
文章分类
社区版块
存档分类
最新评论

JQuery调用WebService

 
阅读更多

前台:

<script type= " text/javascript " >
    $(function () {
        $( " #btn1 " ).click(function () {            // 不带参数
            $.ajax({
                type:  " POST " ,
                url:  " WebService1.asmx/HelloWorld " ,
                dataType:  " json " ,
                contentType:  " application/json; charset=utf-8 " ,
                success: function (json) { alert(json.d); },
                error: function (error) {
                    alert( " 调用出错 "  + error.responseText);
                }
            });
        });
        $( " #btn2 " ).click(function () {            // 带参数
            $.ajax({
                type:  " POST " ,
                url:  " WebService1.asmx/ReturnString " ,
                data:  " {s:' "  + $( " #txt " ).val() +  " '} " ,
                dataType:  " json " ,
                contentType:  " application/json; charset=utf-8 " ,
                success: function (json) { alert(json.d); },
                error: function (error) {
                    alert( " 调用出错 "  + error.responseText);
                }
            });
        });
        $( " #btn3 " ).click(function () {            // 对象
            $.ajax({
                type:  " POST " ,
                url:  " WebService1.asmx/ReturnDomain " ,
                data:  " {} " ,
                dataType:  " json " ,
                contentType:  " application/json; charset=utf-8 " ,
                success: function (json) { alert(json.d.Name + json.d.Age); },
                error: function (error) {
                    alert( " 调用出错 "  + error.responseText);
                }
            });
        });
        $( " #btn4 " ).click(function () {            // 对象集合
            $.ajax({
                type:  " POST " ,
                url:  " WebService1.asmx/ReturnList " ,
                data:  " {} " ,
                dataType:  " json " ,
                contentType:  " application/json; charset=utf-8 " ,
                success: function (json) {
                    $.each(json.d, function (key, value) {
                        alert(value.Name + value.Age);
                    });
                },
                error: function (error) {
                    alert( " 调用出错 "  + error.responseText);
                }
            });
        });
         // 返回DataTable(XML)
        $( ' #btn5 ' ).click(function () {
            $.ajax({
                type:  " POST " ,
                url:  " WebService1.asmx/ReturnXML " ,
                data:  " {} " ,
                dataType:  ' xml ' // 返回的类型为XML
                success: function (result) {
                     try  {
                        $(result).find( " xmltest " ).each(function () {
                            alert($( this ).find( " Name " ).text() +  "   "  + $( this ).find( " Age " ).text());
                        });
                    }
                     catch  (e) {
                        alert(e);
                         return ;
                    }
                },
                error: function (result, status) {  // 如果没有上面的捕获出错会执行这里的回调函数
                     if  (status ==  ' error ' ) {
                        alert(status);
                    }
                }
            });
        });
    });

</script>

<div>
    <p>
        <input type= " button "  id= " btn1 "  value= " 不带参 "  />
    </p>
    <p>
        <input type= " button "  id= " btn2 "  value= " 带参 "  />
        <input type= " text "  id= " txt "  /></p>
    <p>
        <input type= " button "  id= " btn3 "  value= " 对象 "  />
    </p>
    <p>
        <input type= " button "  id= " btn4 "  value= " 集合 "  />
    </p>
    <p>
        <input type= " button "  id= " btn5 "  value= " XML "  />
    </p>

</div> 

后台WebService:

///   <summary>
///  WebService1 的摘要说明
///   </summary>
[WebService(Namespace =  " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem( false )]
//  若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public  class  WebService1 : System.Web.Services.WebService
{

     public  class  User
    {
         public  string  Name
        {
             get ;
             set ;
        }
         public  int  Age
        {
             get ;
             set ;
        }
    }
     ///   <summary>
    
///  返回字符串
    
///   </summary>
    
///   <returns></returns>
    [WebMethod]
     public  string  HelloWorld()
    {
         return  " Hello World " ;
    }
     ///   <summary>
    
///  带参数
    
///   </summary>
    
///   <param name="s"></param>
    
///   <returns></returns>
    [WebMethod]
     public  string  ReturnString( string  s)
    {
         return  s;
    }
     ///   <summary>
    
///  返回对象
    
///   </summary>
    
///   <returns></returns>
    [WebMethod]
     public  User ReturnDomain()
    {
         return  new  User { Name =  " 王斌 " , Age =  11  };
    }
     ///   <summary>
    
///  返回集合对象
    
///   </summary>
    
///   <returns></returns>
    [WebMethod]
     public  List<User> ReturnList()
    {
         return  new  List<User> {
                                 new  User{Name= " 王斌 " ,Age= 23 },
                                 new  User{Name= " 李攀 " ,Age= 11 }
        };
    }
     ///   <summary>
    
///  返回XML
    
///   </summary>
    
///   <returns></returns>
    [WebMethod]
     public  System.Data.DataTable ReturnXML()
    {
        System.Data.DataTable dt =  new  System.Data.DataTable();
        dt.Columns.Add( " Name " );
        dt.Columns.Add( " Age " typeof ( int ));
        DataRow dr = dt.NewRow();
        dr[ 0 ] =  " wangbin " ;
        dr[ 1 ] =  11 ;
        dt.Rows.Add(dr);
        DataRow dr1 = dt.NewRow();
        dr1[ 0 ] =  " lipan " ;
        dr1[ 1 ] =  22 ;
        dt.Rows.Add(dr1);
        dt.TableName =  " xmltest " ;
         return  dt;
    }

} 


转载:http://blog.csdn.net/thebesttome/article/details/6921407

6
7
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics