関連する多くの質問を見てきましたが、jquery を介してパラメーターをページ メソッドに戻すコードの簡単な例はどこにも見つかりませんでした。
encosia.com でいくつかの例を調べました (ありがとう、Dave) が、まだ機能させることができませんでした。これまでのコードは次のとおりです。
更新 - パラメータを渡すコードを追加しましたが、まだ機能していません。
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 メソッドは呼び出されません。ただし、私も持っている場合:
<WebMethod()> _
Public Shared Function RunTest() As String
Return "no params here"
End Function
次に、2 番目のメソッドが呼び出されます。
私は何が欠けていますか?これは間違ったアプローチですか?