2011-04-05 53 views
2

我需要通过使用jQuery调用PageMethod来设置几个Session vars。在PageMethod中设置会话(asp.net)

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

任何人都可以看到我是否错过了什么?

日Thnx

回答

4

你,因为空被传递给将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"]; 
} 
0

是你的变量传递给你的方法是否正确?我将调试并逐步完成,以确保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"); 
     } 
    }); 

不知道他们会帮助,但可能值得一试。