【问题标题】:Get variables in c# from ajax call从ajax调用获取c#中的变量
【发布时间】:2010-06-16 19:21:38
【问题描述】:

我有一个用于登录的 Ajax 调用,代码如下:

//if MOUSE class is clicked
$('.mouse').click(function () {
    //get the form to submit and return a message
    //how to call the function 

    var name = $('#name').val();
    var pwd2 = $('#pwd2').val();
    $.ajax({
    type:"POST",               
     url: "http://localhost:51870/code/Login.aspx",
     data: "{ 'name':'" + $('#name').val() + "', 'pwd':'" + $('#pwd2').val() + "' }",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     context: document.body, 
     success: function () {
        //$(this).addClass("done");
        $(this).hide();
         $('.mouse, .window').hide();
                         }
});

});

问题是我似乎无法在登录页面的 preinit 事件或页面加载事件中捕获 name 和 pwd 变量这是 c# 中的代码:

 protected void Page_PreInit(object sender, EventArgs e)
{
    //taking javascript argument in preinit event
    //from here I'll have to build the page for specific lookbook
    var name = Request.QueryString["name"];
    var pwd = Request.QueryString["pwd"];

}

protected void Page_Load(object sender, EventArgs e)
{
    var name = Request.QueryString["name"];
    var pwd = Request.QueryString["pwd"];
    SignIn(name);
}

我似乎无法在 c# 端获取用户名和密码,不胜感激。

这是我最终的 javascript 代码 c# 代码保持不变:

<script type="text/javascript">

        $(document).ready(function () {    
 //if MOUSE class is clicked
            $('.mouse').click(function () {

                var name = $('#name').val();
                var pwd = $('#pwd').val();
                $.ajax({
               url: "http://localhost:51870/code/Login.aspx?name="+ name +"&pwd="+pwd,
               context: document.body, 
               success: function () {
                    //$(this).addClass("done");
                    $(this).hide();
                     $('.mouse, .window').hide();
                                     }
            });

            });

     });
</script>

谢谢扎卡里

【问题讨论】:

    标签: c# jquery ajax asp.net-ajax


    【解决方案1】:

    您将 JSON 数据发送到登录页面,但您尝试提取 GET/POST 参数。如果您想这样做,您应该将键/值附加到 URL (url: http://localhost:51870/code/Login.aspx?Name=XXXX&Password=YYYYY) 或使用这种格式 (data: Name=XXXX&Password=YYYYY) 传递数据。

    【讨论】:

      猜你喜欢
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多