我只需要在页面加载完成时运行一个方法。
这是我正在使用的js:
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "GET",
url: document.URL + "/LoadAssignees"
});
});
</script>
这是我尝试运行的方法:
[WebMethod()]
protected void LoadAssignees()
{
//load assignees
var sw = Stopwatch.StartNew();
sw.Start();
cboAssignees.DataSource = Uti.GetAssigneeList(_currentUser, 1, false);
sw.Stop();
cboAssignees.DataTextField = "Name";
cboAssignees.DataValueField = "Guid";
cboAssignees.DataBind();
Response.Write(string.Format("Assignee load time: {0}", sw.ElapsedMilliseconds));
}
我觉得我做得对。我没有收到任何错误,但什么也没有发生。我在该方法上设置了一个断点,但它没有被击中,所以我猜测我的 ajax 调用不正确。我错过了什么?
顺便说一句,我想做的是在页面渲染完成后立即异步运行一个方法。如果您能提出更好的方法来实现这一目标,那对我也有用:) 谢谢。
最佳答案
是的,看起来您的访问修饰符限制太多:
Methods within a class that have this attribute set are called XML Web service methods. The method and class must be public and running inside an ASP.NET Web application.
来自此处的“备注”部分:http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.aspx
关于jquery - 如何在页面加载时从 jquery 调用不带参数的页面方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11872457/