首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

为啥数据发送不到后台呢

2012-12-29 
为什么数据发送不到后台呢?这个是存放在页面中的script typetext/javascript$(document).ready(funct

为什么数据发送不到后台呢?
这个是存放在页面中的

    <script type="text/javascript">
        $(document).ready(function () {
            $("#username").blur(function () {
                var name = document.getElementById("username").value;
                var theName = escape(name);
                $.post("A2.ashx", { username: theName },
                    function (data) {
                        alert(data);
                    });
            });
        });
    </script>


下面是ashx的,为什么在ashx设置了断点,每次运行都跑不到ashx的断点呢
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string name = context.Request.QueryString["username"];
        string result = "denied";
        if (name == "t")
        {
            result = "okay";
        }
        context.Response.Write(result);
    }


[解决办法]
你是在vs中调试还是通过iis?

通过iis需要附加aspnet_wp.exe进程才行。参考:VS2008无法断点调试程序

并且你指定为post提交,用get方法获取不到数据的
        $(document).ready(function () {
            $("#username").blur(function () {
                var name = document.getElementById("username").value;
                //var theName = escape(name);//这里不用编码了,指定数据为json对象jq会自动调用encodeURIComponent帮你编码的
                $.post("A2.ashx", { username: theName },
                    function (data) {
                        alert(data);
                    });
            });
        });


    public void ProcessRequest (HttpContext context) { 


        context.Response.ContentType = "text/plain";
        //string name = context.Request.QueryString["username"];
        string name = context.Request.Form["username"];//POST提交用Form
        string result = "denied";
        if (name == "t")
        {
            result = "okay";
        }
        context.Response.Write(result);
    }

热点排行