我正在尝试使用jQuery直接调用ASP.NET AJAX页面方法。我使用encosia.com作为参考。我的内联javascript是
<script type="text/javascript">
$(document).ready(function() {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function() {
$.ajax({
type: "POST",
url: "Default.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
});
});
</script>
<div id="Result">Click here for the time.</div>
我的webmethod
<WebMethod()> _
Public Shared Function GetDate() As String
Return DateTime.Now.ToString()
End Function
我会使用FF并检查发送的POST,但是因为我目前只有IE 7,所以这有点难。其他相关信息,ASP.net 2.0。有谁知道我做错了什么?
web.config - 预先存在仍无法正常工作
<httpModules>
<remove name="FormsAuthentication" />
<remove name="PassportAuthentication" />
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</httpModules>
答案 0 :(得分:5)
由于您使用的是ASP.NET 2.0,因此需要安装ASP.NET AJAX Extensions,如Joe Enos所述。我在这里有关于必要配置工作的更多信息:http://encosia.com/asmx-scriptservice-mistakes-installation-and-configuration/
此外,响应周围的.d
包装器为an addition that didn't come until ASP.NET 3.5。因此,即使您已经完成了其他所有工作,您的msg.d
将在ASP.NET 2.0中undefined
。省略.d
,然后点击:
success: function(msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg);
}
答案 1 :(得分:1)
在.NET 2.0中,您需要确保安装ASP.NET AJAX扩展。在.NET 3.5和4.0中,encosia解决方案无需任何修改即可运行。
我不记得你需要安装什么,但它可能是this。
答案 2 :(得分:0)
您是否检查过web.config以允许页面方法?
<system.web>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
</system.web>