【发布时间】:2020-06-23 09:30:32
【问题描述】:
我有一个奇怪的问题,我在一个 webform 类中有两个 webmethod,它们被 JQuery AJAX 调用,但我得到一个未知的 web 方法错误。我曾尝试将 webmethods 设为公开和静态,但无法使其正常工作。我正在本地测试这个。有人可以帮忙吗,谢谢!
aspx
public partial class PBXservice : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public class PBXservices : System.Web.Services.WebService
{
[WebMethod]
public static string service(string data)
{
//connect client to url where SignalR server is listening
var hubConnection = new HubConnection("http://localhost:8081/");
var serverHub = hubConnection.CreateHubProxy("WebHub");
//start Hub and wait to finish connecting
hubConnection.Start().Wait();
//call Hub with Invoke, pass name and argument
serverHub.Invoke("Connect", "PBXWebApp");
// string line = null;
while ((data = System.Console.ReadLine()) != null)
{
// send message to the server
serverHub.Invoke("SendPBXData", data, "PBXWebApp").Wait();
}
return data;
}
[System.Web.Services.WebMethod]
public static string GetData()
{
return DateTime.Now.ToString();
}
}
}
AJAX
var pbxdata = "Hi there!";
jQuery.ajax({
url: 'PBXservice.aspx/service',
type: "POST",
dataType: "json",
data: "{'pbxdata': '" + pbxdata + "'}",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(JSON.stringify(data));
}
});
$.ajax({
type: "POST",
url: "PBXservice.aspx/GetData",
data: '',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
},
});
网络配置
<?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>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<!--
For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.
The following attributes can be set on the <httpRuntime> tag.
<system.Web>
<httpRuntime targetFramework="4.5" />
</system.Web>
-->
<system.web>
<compilation debug="true" targetFramework="4.5" />
<pages controlRenderingCompatibilityVersion="4.0" />
</system.web>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer></configuration>
【问题讨论】:
-
转到 jQuery 文档页面 here 并确保您发送的
data、datatype和contentType的值正确。 -
另外,在使用代码之前,请使用 chrome 或 postman 之类的工具调用 web 方法,并确保您可以调用它们,获得响应,确保响应符合预期格式。这将帮助您排除故障。一旦你消除了所有的皱纹,然后编写你的 JavaScript 代码来调用 web 方法。
-
CodingYoshi 感谢您的回复,我添加第二个 webmethod 的原因是为了测试 jquery 数据属性,但在那个 ajax 调用中数据是空白的。
-
这会影响您的网络方法吗?
-
否,因为我正在获取异常详细信息:System.ArgumentException:未知的 Web 方法服务。参数名称:methodName