【发布时间】:2011-05-19 09:36:36
【问题描述】:
我设置了一个页面方法并尝试通过 jQuery 调用它。有时我到达成功函数,有时到达错误函数,对我来说似乎是随机的。在任何情况下,响应都包含整个页面标记。
我尝试同时使用$.ajax 和ScriptManager,结果相同。
我也在这里尝试过这个想法:Call ASP.NET PageMethod/WebMethod with jQuery - returns whole page 什么都没有。
这是 JavaScript 代码:
$(document).ready(function() {
$(".tree").dynatree({
onActivate: function(node) {
$('#title').val(node.data.title);
$.ajax({
type: "POST",
url: window.location.href + "/GetData",
data: "{'ID':22}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) { alert(response); },
error: function() { alert('Error!'); }
});
}
});
});
这里是c#代码:
[WebMethod]
public static string GetData(string ID)
{
if (string.IsNullOrEmpty(ID))
throw new Exception("No ID passed!");
return "Test";
}
编辑:好吧,我搞定了。我将参数类型从 int 更改为 string,现在调用该方法。我可以稍后再执行 int.Parse,但为什么会发生这种情况?
【问题讨论】:
-
当您使用生成的客户端代理时,它是否工作(即 PageMethods.GetData(...)?)
-
@dfowler - 不。结果相同。
-
你的路由或任何可能与 url 混淆的东西吗?