Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# webform localhost中对webmethods的AJAX调用导致未知web方法错误_C#_Jquery_Ajax_Webforms - Fatal编程技术网

C# webform localhost中对webmethods的AJAX调用导致未知web方法错误

C# webform localhost中对webmethods的AJAX调用导致未知web方法错误,c#,jquery,ajax,webforms,C#,Jquery,Ajax,Webforms,我有一个奇怪的问题,在一个webform类中有两个webmethods,它们被jQueryAjax调用,但我得到一个未知的web方法错误。我已经尝试过让webmethods公开和静态,但我无法让它工作。我正在本地测试这个。有人能帮忙吗,谢谢 aspx 阿贾克斯 网络配置 <?xml version="1.0" encoding="utf-8"?> <!-- For more information on how to configure your ASP.NET appli

我有一个奇怪的问题,在一个webform类中有两个webmethods,它们被jQueryAjax调用,但我得到一个未知的web方法错误。我已经尝试过让webmethods公开和静态,但我无法让它工作。我正在本地测试这个。有人能帮忙吗,谢谢

aspx

阿贾克斯

网络配置

<?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>

早上好。你真的需要这个“PBXservices”类吗?您可以将“service”和“GetData”方法直接放在“PBXservice”中。然后更改“App\u Code\RouteConfig.cs”中的配置: 旧:“settings.AutoRedirectMode=RedirectMode.Permanent” 新建:“settings.AutoRedirectMode=RedirectMode.Off”

ASPX.CS

public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // Method intentionally left empty. } [WebMethod] public static string GetData() { return DateTime.Now.ToString(); } } 公共部分类\u默认值:System.Web.UI.Page { 受保护的无效页面加载(对象发送方、事件参数e) { //方法故意留空。 } [网络方法] 公共静态字符串GetData() { return DateTime.Now.ToString(); } } ASPX(HTML)

$(文档).ready(函数(){ $('#bntAjax')。单击(CallAjax); }); 函数CallAjax(){ $.ajax({ 类型:“POST”, url:“PBXservices/GetData”, 数据:“”, contentType:“应用程序/json;字符集=utf-8”, 数据类型:“json”, 成功:功能(响应){ 控制台日志(响应); }, }); } 应用程序代码\RouteConfig.cs

public static class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { var settings = new FriendlyUrlSettings(); settings.AutoRedirectMode = RedirectMode.Off; routes.EnableFriendlyUrls(settings); } } 公共静态类RouteConfig { 公共静态无效注册表项(路由收集路由) { var settings=new FriendlyUrlSettings(); settings.AutoRedirectMode=RedirectMode.Off; routes.EnableFriendlyURL(设置); } }
我不得不将Codebehind属性更改为CodeFile,就是这样

CodeBehind=“PBXservice.aspx.cs”

CodeFile=“PBXservice.aspx.cs”


谢谢大家发帖

转到jQuery文档页面,确保为
数据
数据类型
内容类型
发送了正确的值。此外,在使用代码之前,请使用诸如chrome或postman之类的工具调用web方法,并确保可以调用它们,获得响应,确保响应为预期格式。这将帮助您排除故障。一旦你解决了所有的问题,然后编写JavaScript代码来调用web方法。CodingYoshi谢谢你的回复,我添加第二个webmethod的原因是为了测试jquery数据属性,但在该ajax调用中,数据保留为空。这是否影响了您的web方法?否,因为我正在获取异常详细信息:System.ArgumentException:未知web方法服务。参数名称:MethodName Hi Anderson我已经删除了子脚本,但是我必须添加RouteConfig文件吗?因为目前我没有App_代码文件夹,注册友好URL可以做什么来改变呢?你的项目是webforms吗?问题解决了吗? public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // Method intentionally left empty. } [WebMethod] public static string GetData() { return DateTime.Now.ToString(); } } $(document).ready(function () { $('#bntAjax').click(CallAjax); }); function CallAjax() { $.ajax({ type: "POST", url: "PBXservices/GetData", data: '', contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { console.log(response); }, }); } public static class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { var settings = new FriendlyUrlSettings(); settings.AutoRedirectMode = RedirectMode.Off; routes.EnableFriendlyUrls(settings); } }