Jquery 在PageMethod(asp.net)中设置会话
我需要通过使用jQuery调用PageMethod来设置几个会话变量 客户端js如下所示:Jquery 在PageMethod(asp.net)中设置会话,jquery,asp.net,session,pagemethods,Jquery,Asp.net,Session,Pagemethods,我需要通过使用jQuery调用PageMethod来设置几个会话变量 客户端js如下所示: function setSession(Amount, Item_nr) { //alert(Amount + " " + Item_nr); var args = { amount: Amount, item_nr: Item_nr } //alert(JSON.stringify(passingArguments)
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;
}
服务器端是这样的:
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;
}
只是,似乎没有设置会话变量。当我试图回应时,写出会话变量,我什么也得不到。我没有收到任何错误,我可以提醒从onclick事件传递到js函数的值,因此它们就在那里
有人能看到我是否遗漏了什么吗
Thnx由于将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:
c
变量是否正确地传递给方法?我将调试并逐步执行它,以确保amount和item_nr能够进入您的服务器端方法。如果这是一个问题,您可能需要考虑单独传递参数,或者可能将Ajax帖子的类型设置为传统: 示例: 不确定他们是否会有所帮助,但可能值得一试
$.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");
}
});