【发布时间】:2013-11-06 09:34:54
【问题描述】:
我知道有很多这样的问题,但我无法用这些答案解决我的问题。
我有基本页面:
<body>
<form id="form1" runat="server">
<div id="Result">
<asp:ScriptManager ID="ScriptManager1"
EnableScriptGlobalization="true"
EnableScriptLocalization="true"
EnablePageMethods="true"
EnablePartialRendering="true" runat="server" />
<script type="text/javascript">
$(document).ready(function () {
$("#txtSearch").bind("change", search);
});
function test() {
alert("asdadadaads");
}
function search() {
$.ajax({
type: "POST",
url: "~/Search.aspx/GetRegion",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d)
}
});
}
</script>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
</div>
</form>
我可以调用测试函数并显示警报。同样在 Firebug 中,我看到 search() 函数也被调用,但我无法进入 WebMethod 后面的代码。
WebMethod 是这样的:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static void GetRegion()
{
SearchService.Service1 client = new Service1();
}
我对此感到很疯狂,所以如果你能提供帮助,谢谢:)
【问题讨论】:
-
从 url 中删除 ~/ 使其成为 url: "Search.aspx/GetRegion" 并在 webmethod 中返回字符串
-
首先,从 ajax url 中删除波浪号 (~)。这适用于服务器端 ASP.Net,在其他任何地方都不起作用。