当前位置:编程学习 > JS >>

c# 后台取jQuery Ajax中的复杂参数问题

页面文件

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestjQueryajax.aspx.cs" Inherits="OA.Web.Test.TestjQueryajax" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=" http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../JS/JQuery/jquery-1.4.4.js" type="text/javascript"></script>
    <script type="text/javascript">
        function TestjQueryAjax() {
            $.ajax({
                type: "post",
                dataType: "text",
                contentType: "application/json",
                data: {data:5,name:"admin"},
                url: "TestjqueryajaxHandler.ashx",
                success: function (result) { alert(result); },
                error: function (e) { alert(e.responseText); }
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="button" value="testjqueryajax" onclick="TestjQueryAjax()" />
    </div>
    </form>
</body>
</html>
后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Script.Serialization;


namespace OA.Web.Test
{
    /// <summary>
    /// TestjqueryajaxHandler 的摘要说明
    /// </summary>
    public class TestjqueryajaxHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            byte[] datas = new byte[context.Request.ContentLength];
            context.Request.InputStream.Read(datas, 0, context.Request.ContentLength);
            string str = Encoding.UTF8.GetString(datas);

            context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

//取到 data=5&name=admin 这么个东西怎么搞啊

答案:一、取session的值

1.导入using System.Web.SessionState;

2.实现IRequiresSessionState接口

3.object obj= context.Session["xxx"];

-------------------------我是很长的分割线--------------------------------

二、取参数的值

string str= context.Request["xxxx"];

上一个:vs2008是否自带AJAX控件?
下一个:Ajax中readyState返回值为0

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,