2009-07-27 4 views
0

많은 질문이 있지만 어디에도 jquery를 통해 페이지 메서드에 매개 변수를 전달하는 간단한 코드 예제가 있습니다.ASP.NET 폼 : jquery를 사용하여 매개 변수가있는 asp.net 페이지 메서드에 게시

encosia.com (감사합니다, Dave)에서 몇 가지 예제를 살펴 보았지만 여전히 작동하지 않았습니다. 지금까지 코드는 다음과 같습니다 :

업데이트 됨 - params를 전달하는 코드가 추가되었지만 여전히 작동하지 않습니다.

에서 Test.aspx :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>test page</title> 

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 

    <script type="text/javascript"> 
     $(document).ready(function() { 
     // Add the page method call as an onclick handler for the div. 
     $("#Result").click(function() { 
      var intxt = document.getElementById("<%= txtIn.ClientID %>").value; 
      var params = $.toJSON({ 'inStr': intxt }); 
      $.ajax({ 
      type: "POST", 
      url: "test.aspx/RunTest", 
      data: params, 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function(msg) { 
       $("#Result").text(msg); 
      } 
     }); 
     }); 
     }); 
    </script> 

</head> 
<body> 
    <form id="form1" runat="server"> 
    <div id="Result">Click here</div> 
    <asp:TextBox ID="txtIn" runat="server"></asp:TextBox> 
    </form> 
</body> 
</html> 

및 test.aspx.vb에

:

<WebMethod()> _ 
    Public Shared Function RunTest(ByVal instr As String) As String 
     Return "this is your string: " + instr 
    End Function 

업데이트 :

RunTest가 호출되지 않습니다 위의 menthod. 그러나, 내가 가지고있는 경우 :

<WebMethod()> _ 
    Public Shared Function RunTest() As String 
     Return "no params here" 
    End Function 

그러면 두 번째 방법이 호출됩니다.

무엇이 누락 되었습니까? 이것은 잘못된 접근입니까?

답변

2

매개 변수는 대/소문자를 구분합니다. 귀하의

var params = $.toJSON({ 'inStr': intxt }); 

는 메소드 서명과 일치하지 않습니다 :

Public Shared Function RunTest(ByVal instr As String) As String 

그 나머지는 올바른 보인다.

일반적으로 $ .toJSON과 같은 다른 플러그인 대신 using json2.js' JSON.stringify() to serialize JSON on the client-side을 제안합니다. json2.js 'API는 새로운 브라우저가 구현중인 브라우저 고유의 JSON 지원 (지금까지 IE8 및 Fx3.5)을위한 ECMA 표준과 일치합니다. 따라서 JSON.stringify()를 사용하여 객체를 직렬화하면 해당 브라우저에서 더 빠르고 고유 한 기능으로 자동 업그레이드됩니다.

+0

감사합니다 @Dave Ward, 나는 또한 그것에 stucked 및 params 대소 문자를 구분 한 후 문제를 극복. 많이 고마워!. –

관련 문제