zoukankan      html  css  js  c++  java
  • ajax 以json 的形式来传递返回参数的实例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestWcf.aspx.cs" Inherits="WebApplication1.TestWcf" %>
    <!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>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input type="button"  id="send"  value="请求" />
            <div id="resText">
            </div>
            
        </div>
        </form>
    </body>
    </html>
    <script src="Scripts/jquery-1.10.2.min.js"></script>
    <script src="Scripts/jquery-1.10.2.js"></script>
    <script type="text/javascript"  >
      
        $(function () {
            $('#send').click(function () {
                //alert("111");
               
                //获取参数
                var PostParam = {
                    'UserInfo': JSON.stringify({
                        'Name': escape("花千骨"),  //  'Name': escape($("#Name").val()),
                        'NickName':"dddd",
                        'Email':"moloic@163.com",
                        'CreateTime': "2015-12-12"
                       
                    })
                }
                $.ajax({
                    type: "GET",  //POST
                    url: "UserInfoHandler.ashx",
                    data: PostParam,
                    dataType: "json",
                    success: function (data) {
                      //  alert("sss");
                      //  debugger;
                        $('#resText').empty();   //清空resText里面的所有内容
                        var html = '';
                        $.each(data, function (commentIndex, comment) {
                            html += '<div class="comment"><h6>' + comment['Name']
                                      + ':</h6><p class="para"' + comment['Email']
                                      + '</p></div>';
                        });
                        $('#resText').html(html);
                    }
                });
            });
        });
    </script>

     ---------------------------------------------后台处理-----------------------------------------------------------------------------------

    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    namespace WebApplication1
    {
        /// <summary>
        /// UserInfoHandler 的摘要说明
        /// </summary>
        public class UserInfoHandler : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write(getUserList( context));
            }
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
            public string getUserList(HttpContext context)
            {
               
                TestModel.UserInfo UserInfo = new TestModel.UserInfo();
                //获取参数
                UserInfo = (TestModel.UserInfo)JsonConvert.DeserializeObject(context.Request["UserInfo"].ToString(), typeof(TestModel.UserInfo));
                UserInfo.Name=  HttpUtility.UrlDecode(UserInfo.Name);
               
          
                List<TestModel.UserInfo> userinfoList = new List<TestModel.UserInfo>();
                //加载数据
                try
                {
                    ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
                    IList<TestModel.UserInfo> ilist = new List<TestModel.UserInfo>();
                    ilist = service.getUserInfoData(UserInfo);
                    userinfoList = IListToList<TestModel.UserInfo>(ilist);
                }
                catch (Exception ex)
                {
                    userinfoList = null;
                }
                return JsonConvert.SerializeObject(userinfoList);
                
            }
            /// <summary>
            /// ILIST 转换LIST 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="list"></param>
            /// <returns></returns>
            public List<T> IListToList<T>(IList<T> list)
            {
                T[] array = new T[list.Count];
                list.CopyTo(array, 0);
                return new List<T>(array);
            }
        }
    }
  • 相关阅读:
    最大并发连接数和最大会话数的区别
    Redis Sentinel 情况下bind地址设置
    ZooKeeper 授权验证
    推荐一个zookeeper信息查看工具
    WebForm-博客园-6.0-空间(Space)-短信息(Msg)
    ylbtech-cnblogs(博客园)-数据库设计-6.0-Msg(短消息)
    WebForm-博客园-1.0-账户模块(Passport)-登录与注册
    WebForm+Web.config: 超时时间已到。在操作完成之前超时时间已过或服务器未响应。
    ylbtech-cnblogs(博客园)-数据库设计-1,Passport(账户)
    IIS 配置
  • 原文地址:https://www.cnblogs.com/dullbaby/p/4791261.html
Copyright © 2011-2022 走看看