zoukankan      html  css  js  c++  java
  • ajax调用webService中的方法

    页面代码

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <script src="jquery-1.7.2.js"></script>
        <script type="text/javascript">
          onload=function(){
                //web service的调用
                $.ajax({
                    type: "get",
                    contentType: 'application/json',    
                    url: "http:www.LCWeb.com/WebService.cs/HelloWorld",
                    dataType: "xml",
                    success: function (data) {
                        alert(data);
                    }
                });
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div id="div1">
            </div>
        </form>
    </body>
    </html>
    

     Web Service中的代码

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.Services;
    
    /// <summary>
    /// WebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://microsoft.com/webservices/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {
    
        public WebService()
        {
    
            //如果使用设计的组件,请取消注释以下行 
            //InitializeComponent(); 
        }
    
        [WebMethod]
        public List<Student> HelloWorld()
        {
            //编写连接字符串
            string conStr = "Data Source=.;Initial Catalog=school;Integrated Security=True";
            //创建连接对象
            SqlConnection conn = new SqlConnection(conStr);
            //编写sql语句
            string sqlStr = "select * from student";
            //创建适配器对象
            SqlDataAdapter adapter = new SqlDataAdapter(sqlStr, conn);
            //创建临时表对象
            DataTable dt = new DataTable();
            //填充表数据
            adapter.Fill(dt);
            List<Student> stuList = new List<Student>();
            //将数据封装成集合
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                stuList.Add(new Student
                {
                    Id = Convert.ToInt32(dt.Rows[i]["id"].ToString()),
                    Name = dt.Rows[i]["Name"].ToString(),
                });
            }
            return stuList;
        }
    
    }
    

     实体类中的代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Student 的摘要说明
    /// </summary>
    public class Student
    {
    	public Student()
    	{
    		//
    		// TODO: 在此处添加构造函数逻辑
    		//
    	}
        public int Id { get; set; }
        public String UserName { get; set; }
        public String Pwd { get; set; }
        public string Name { get; set; }
    }
    
  • 相关阅读:
    php require与include的区别
    php页面消耗内存过大处理办法
    flex if(a is b){},flex if(a as b){} 意思
    thinkphp的配置文件修改后不起作用
    ThinkPHP 入口文件定义 3.0 输出两次解决办法
    JS中的Math.ceil和Math.floor函数
    ThinkPHP 配置问题
    Google 快照无法使用时,如何恢复
    首页被收录了,但是只显示网址,处理办法
    RED5 端口使用
  • 原文地址:https://www.cnblogs.com/liuchang/p/3315639.html
Copyright © 2011-2022 走看看