2011-04-05 2 views
2

jQuery를 사용하여 PageMethod를 호출하여 몇 가지 Session 변수를 설정해야합니다. 이 같은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; 
} 

아니라,이 세션 바르가 설정되어 있지 않은 것 같다. 세션 vars를 Response.Write하려고하면 아무것도 얻지 못합니다. 오류가 없으며 onclick 이벤트에서 js 함수로 전달 된 값을 경고 할 수 있으므로 해당 값이 있습니다.

내가 놓친 사람이 있습니까?

Thnx

답변

4

당신의 널이 그것에서 오는 위치를 확인하기 위해 자바 스크립트와 C#을 단계별로 디버거를 사용의 WebMethod에 전달되고 있기 때문에 세션에서 아무것도 못하고.

빠른 테스트 페이지에서 작동하도록 게시 한 코드는 괜찮아 보이므로 문제는 코드의 다른 부분에 있습니다. 여기 내 테스트 코드가 도움이 되길 바랍니다.

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 :

[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

이 변수가 올바르게 방법에 전달되는

<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#을? 디버그하고 단계별로 수행하여 amountitem_nr이 서버 측 메소드를 사용하고 있는지 확인합니다.

예 : 그 문제는 경우에 당신은 개별적으로 인수 전달 (또는 가능 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"); 
     } 
    }); 

확실하지 않음을 그들이 도움이 될 것입니다하지만 만약 수도 시도해 볼만하다.