在 PageMethod (asp.net) 中设置会话
我需要通过使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在会话中没有得到任何内容,因为 null 被传递给 webmethod,请使用调试器单步执行 javascript 和 c# 以查看其来自何处。
您发布的代码似乎没问题,因为我设法让它在快速测试页面中工作,所以问题出在代码的其他位置。这是我的测试代码,希望对您有所帮助。
jquery:
html:
c#
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:
html:
c#
您的变量是否正确传递给您的方法?我将调试并逐步执行它,以确保
amount
和item_nr
使其进入您的服务器端方法。如果这是一个问题,您可能需要考虑单独传递参数(或者可能将 ajax post 的类型设置为传统):示例:
不确定它们是否有帮助,但也许值得一试。
Are your variables being passed to your Method properly? I would debug and step through it to ensure that
amount
anditem_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 totraditional
:Examples:
Not sure if they will help but might be worth a try.