【发布时间】:2015-11-26 13:20:37
【问题描述】:
我正在开发一个相当大的应用程序。但是我在 Ajax 中遇到了问题。为此,我尝试制作一个新的简短程序来调用 Ajax 以进行测试。但我陷入了困境。 这是Test.aspx代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Testing</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="test.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
Ajax 函数是
$(document).ready(function () {
$("#Button1").click(function () {
var text = $("#TextBox1").val();
text = "this is test";
debugger
$.ajax({
type: "POST",
contentype: "application/json; charset=utf-8",
url: "Test.aspx/Test",
data: { str: text},
//dataType:"json",
success: function (data) {
alert("yes");
},
error: function (response) {
debugger
alert(response);
}
});
return false;
});
});
下面是Test.aspx.cs代码
[WebMethod]
public void Test(string str)
{
Console.WriteLine(str);
}
当我在 TextBox 中添加一些值时。它会提醒是的!但不调用 [WebMethod]。 任何人都知道这个问题。
【问题讨论】:
-
我改成静态的了。但问题依然存在。
-
在成功回调中使用
Console.WriteLine(str);而不是return str;然后alert(data)
标签: javascript c# jquery asp.net ajax