在 PageMethod (asp.net) 中设置会话

发布于 2024-10-30 20:16:31 字数 1094 浏览 1 评论 0原文

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

客户端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 函数的值,因此它们就在那里。

谁能看看我是否错过了什么?

谢谢

I need to set a couple of Session vars by calling a PageMethod using jQuery.

The client side js looks like this:

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");
           }
       });

     }

and the server side like this:

[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;
}

only, it seems that the Session vars are not set. When I try to Response.Write out the Session vars, I get nothing. I get no errors, and I can alert out the values passed from the onclick event, to the js function, so they are there.

Can anyone see if I missed something?

Thnx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

违心° 2024-11-06 20:16:31

您在会话中没有得到任何内容,因为 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"];
}

Your not getting anything in your session because a null is being passed to the webmethod, use a debugger to step through your javascript and c# to see where its coming from.

The code you posted seems ok as I managed to get it working in a quick test page, so the problem is else where in your code. Here's my test code, hope it helps.

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"];
}
悲凉≈ 2024-11-06 20:16:31

您的变量是否正确传递给您的方法?我将调试并逐步执行它,以确保 amountitem_nr 使其进入您的服务器端方法。如果这是一个问题,您可能需要考虑单独传递参数(或者可能将 ajax post 的类型设置为传统):

示例:

$.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");
       }
   });

不确定它们是否有帮助,但也许值得一试。

Are your variables being passed to your Method properly? I would debug and step through it to ensure that amount and item_nr are making it to your server-side method. If that is an issue you may want to consider passing in your arguments individually (or possibly setting the type of ajax post to traditional:

Examples:

$.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");
       }
   });

Not sure if they will help but might be worth a try.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文