【发布时间】:2012-12-26 01:19:59
【问题描述】:
我尝试在 asp.net 网站(使用 vb.net 2.0)中使用 Ajax(通过 jQuery)。
这是我的 .aspx 页面 (ajax.aspx):
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="ajax.aspx.vb" Inherits="_Ajax" %>
<!DOCTYPE html>
<html>
<head>
<title>Calling page methods with jQuery</title>
<style type="text/css">
#Result {
cursor: pointer;
}
</style>
</head>
<body>
<div id="Result">Click here for the time.</div>
<script src="jquery-1.8.3.js"></script>
<script>
$('#Result').click(function () {
$.ajax({
type: "POST",
url: "ajax.aspx/HelloWorld",
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);
},
error: function (xhr, ajaxOptions, thrownError) {
alert("xhr.responseText= " + xhr.responseText);
alert("xhr.responseStatus= " + xhr.responseStatus);
alert("thrownError= " + thrownError);
}
});
});
</script>
</body>
</html>
这里是代码隐藏文件(ajax.aspx.vb):
Imports System
Imports System.Web.Services
Public Class _Ajax
Inherits System.Web.UI.Page
Protected Sub Page_Load()
End Sub
<WebMethod()> _
Public Shared Function HelloWorld() As String
Return "Hello: "
End Function
End Class
当我点击我的 div 时,ajax 调用失败:
System.ArgumentException:未知的网络方法 HelloWorld。
[编辑]
多次刷新重启浏览器后,发现web方法!
我现在尝试添加新的网络方法,但我再次收到错误“未知网络方法”。
我认为是缓存问题。
你知道如何强制禁用这种缓存吗?
【问题讨论】:
-
您可能缺少一些 web.config 值(httpmodules),您可以看到您可以在页面中访问 scriptmanager。如果没有,您需要为您的应用程序安装 ajax 扩展。
-
你说得对,安装 Ajax for ASP.NET 2.0 后更好(但还不够)
-
你的问题解决了吗?或者你还有一些问题
-
没有解决。我收到错误“未知的网络方法”。我编辑了我的第一篇文章来描述它。
标签: jquery asp.net ajax vb.net