手记

Jquery调用webservice

/// <summary>
    /// RecruitmentHome 的摘要说明
    /// </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 RecruitmentHome : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

 /// <summary>
        /// return company information by comid
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [WebMethod]
        public string getcomname(int id) {
           
            Companybll bll = new Companybll();
            return bll.getinfor(id).Rows[0]["conmanyname"].ToString();
           
        }

 /// <summary>
        /// return jobs' list by comid
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [WebMethod]
        public List<string> getjobinfo(int id)
        {
            List<string> list = new List<string>();
            DataTable dt = new DataTable();
            Companybll bll = new Companybll();
            dt=bll.getjobinfo(id);
            foreach (DataRow dr in dt.Rows)
            {
             
                list.Add("<a href=http://www.xxx.com/JobsInfo.aspx?JobID=" + dr["Ncid"].ToString() + "&ComID="+id+">" + dr["PosName"].ToString() + "</a>");
               // list.Add("<a href='http//:www.xxx.com/JobsInfo/'+dr["Ncid"].ToString()+'/'+id+'.html'>" + dr["PosName"].ToString() + "</a>");
               
            }
            
             return list;
        }

/// <summary>
        /// 返回XML
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public DataSet GetDataSet(int id)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", Type.GetType("System.String"));
            dt.Columns.Add("Value", Type.GetType("System.String"));
            DataRow dr = dt.NewRow();
            dr["ID"] = "1";
            dr["Value"] = "新年快乐";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ID"] = "2";
            dr["Value"] = "万事如意";
            dt.Rows.Add(dr);
            ds.Tables.Add(dt);
            return ds;
        }

 public class Class1
    {
        public string ID { get; set; }
        public string Value { get; set; }
      
    }

}

 

调用:

<script src="../Js/jquery-1.6.4.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#Button1").click(function () {
                try {
                    $.ajax({

                        type: "POST",
                        contentType: "application/json",
                        url: "../RecruitmentHome.asmx/HelloWorld",
                        data: "{}", //注意格式!!"{callerName:abc}",可以,但 "{callerName:"+user+"}"不行,Error!
                        dataType: 'json',
                        success: function (result) {
                            // alert("jQuery callback: " + result);
                            $("#result").html(result.d);
                        }

                    });
                }
                catch (ex) {
                    alert(ex);
                }
            });
        });

        var cid = 3000;
        //获取公司名称
        $(document).ready(function () {
         
                $.ajax({
                    type: "POST",
                    contentType: "application/json",
                    url: "../RecruitmentHome.asmx/getcomname", //get company name
                    data: "{id:'" + cid + "'}",
                    dataType: 'json',
                    success: function (result) {
                        $('#companyname').append(result.d);
                    }
                });

            });

            //获取公司介绍
            $(document).ready(function () {
               
                    $.ajax({
                        type: "POST",
                        contentType: "application/json",
                        url: "../RecruitmentHome.asmx/getcomjianjie", //get company Introduction
                        data: "{id:'" + cid + "'}",
                        dataType: 'json',
                        success: function (result) {
                            $('#cominfo').append(result.d);
                        }
                    });

                });

                //返回职位列表
                $(document).ready(function () {
                  
                        $.ajax({
                            type: "POST",
                            contentType: "application/json",
                            url: "../RecruitmentHome.asmx/getjobinfo",
                            data: "{id:'" + cid + "'}",
                            dataType: 'json',
                            success: function (result) {
                                $(result.d).each(function () {
                                    //alert(this);
                                    $('#getjobinfo').append(this.toString() + " <br/> ");
                                    //alert(result.d.join(" | "));
                                });
                            }
                        });
                  
                });
    </script>
</head>
<body>
<input id="Button1" type="button" value="Invokeweb_test" />
     <div id="result"></div>
  
     company name:<span id="companyname"></span><br/>
     company information:<div id="cominfo" ></div>
     jobs information  <div id="getjobinfo" ></div>
</body>
</html>

1人推荐
随时随地看视频
慕课网APP