为了账号安全,请及时绑定邮箱和手机立即绑定

Jquery调用webservice

2018.09.16 15:57 526浏览

/// <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下载
官方微信

举报

0/150
提交
取消