【问题标题】:Set Session in PageMethod (asp.net)在 PageMethod (asp.net) 中设置会话
【发布时间】:2011-04-06 00:50:31
【问题描述】:

我需要通过使用 jQuery 调用 PageMethod 来设置几个 Session 变量。

客户端js长这样:

function setSession(Amount, Item_nr) {
        //alert(Amount + " " + Item_nr);
        var args = {
            amount: Amount, item_nr: Item_nr
        }
        //alert(JSON.stringify(passingArguments));
       $.ajax({
           type: "POST",
           url: "buycredit.aspx/SetSession",
           data: JSON.stringify(args),
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: function () {
               alert('Success.');
           },
           error: function () {
               alert("Fail");
           }
       });

     }

和服务器端是这样的:

[System.Web.Services.WebMethod(EnableSession = true)]
public static void SetSession(int amount, int item_nr)
{
    HttpContext.Current.Session["amount"] = amount;
    HttpContext.Current.Session["item_nr"] = item_nr;
}

只是,似乎没有设置会话变量。当我尝试 Response.Write 出会话变量时,我什么也没得到。我没有收到任何错误,我可以提醒从 onclick 事件传递给 js 函数的值,所以它们就在那里。

谁能看看我是否遗漏了什么?

谢谢

【问题讨论】:

    标签: jquery asp.net session pagemethods


    【解决方案1】:

    由于将 null 传递给 webmethod,您的会话中没有任何内容,请使用调试器单步执行您的 javascript 和 c# 以查看其来源。

    您发布的代码似乎没问题,因为我设法让它在快速测试页面中运行,所以问题出在您的代码中的其他位置。这是我的测试代码,希望对您有所帮助。

    jquery:

    $(document).ready(function () {
            $('#lnkCall').click(function () {
                setSession($('#input1').val(), $('#input2').val());
                return false;
            });
        });
    
        function setSession(Amount, Item_nr) {
            var args = {
                amount: Amount, item_nr: Item_nr
            }
    
            $.ajax({
                type: "POST",
                url: "buycredit.aspx/SetSession",
                data: JSON.stringify(args),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function () {
                    alert('Success.');
                },
                error: function () {
                    alert("Fail");
                }
            });
    
        }
    

    html:

    <div>
        Input1: <input id="input1" type="text" />
        <br />
        Input2 <input id="input2" type="text" />  
        <br />
        <a id="lnkCall" href="#">make call</a>
    
    
        <br />
        <asp:Button ID="myButton" runat="server" Text="check session contents" onclick="myButton_Click" />
        <br />
        <asp:Literal ID="litMessage" runat="server" />
    
    </div>
    

    c#

    [System.Web.Services.WebMethod(EnableSession = true)]
    public static void SetSession(int amount, int item_nr)
    {
        HttpContext.Current.Session["amount"] = amount;
        HttpContext.Current.Session["item_nr"] = item_nr;
    }
    
    
    protected void myButton_Click(object sender, EventArgs e)
    {
        litMessage.Text = "ammount = " + HttpContext.Current.Session["amount"] + "<br/>item_nr = " + HttpContext.Current.Session["item_nr"];
    }
    

    【讨论】:

      【解决方案2】:

      您的变量是否正确传递给您的方法?我将调试并逐步检查它,以确保 amountitem_nr 将其用于您的服务器端方法。如果这是一个问题,您可能需要考虑单独传递参数(或者可能将 ajax 帖子的类型设置为 traditional

      示例:

      $.ajax({
             type: "POST",
             url: "buycredit.aspx/SetSession",
      
             //Option 1:
             traditional : true, 
      
             //Option 2:
             data: 
             {
                    'amount' : Amount,
                    'item_nr': Item_nr
             },
      
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: function () {
                 alert('Success.');
             },
             error: function () {
                 alert("Fail");
             }
         });
      

      不确定它们是否会有所帮助,但可能值得一试。

      【讨论】:

        猜你喜欢
        • 2023-04-01
        • 1970-01-01
        • 2012-10-13
        • 2016-05-10
        • 1970-01-01
        • 2018-08-22
        • 1970-01-01
        • 2015-03-11
        • 1970-01-01
        相关资源
        最近更新 更多