我制作了一个简单的示例Web项目(使用vs 2015 Empty模板),其中包含一个非常简单的WebMethod。但无论我尝试什么,它都无法正常工作。 我们的想法是,当您将页面滚动到底部时,它会调用Web方法,但是ajax调用总是会失败。
代码背后:
Imports System.Web.Script.Services
Imports System.Web.Services
Public Class deafult
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
<System.Web.Services.WebMethod, ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Shared Function Gettime() As String
Return Now.ToShortDateString
End Function
End Class
设计师代码:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="deafult.aspx.vb" Inherits="test2.deafult" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>testing</title>
</head>
<body>
<form id="form1" runat="server">
<div style="height:750px; background: red">
<br /><br />
</div>
</form>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(window).scroll(function () {
if ($(window).scrollTop() == $(document).height() - $(window).height()) {
GetRecords();
}
});
function GetRecords() {
$.ajax({
type: "POST",
url: "default.aspx/Getime",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert('failed');
}
});
}
function OnSuccess(response) {
alert('done');
}
</script>
</body>
</html>
Web配置:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
</configuration>