Чтобы получить 401 Несанкционированный, когда я отправляю Ajax POST - PullRequest
0 голосов
/ 31 мая 2018

Я тестировал очень простой Ajax-код с типом записи в Visual Studio 2015 ~ 2017,
к сожалению, я всегда получал сообщение об ошибке из браузера, как показано на рисунке
enter image description here

Пожалуйста, помогите мне понять, что не так с этой проблемой, спасибо

PS:
Клиентская сторона

<script src="Scripts/jquery-1.10.2.min.js"></script>
<script>    
    $.ajax({
        type: "POST",
        url: "WebForm1.aspx/Delete",
        data: {},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {    
            alert("=="+ data.d)
        },
        failure: function (response) {
            alert(response.d);
        }    
    });
</script>

Серверная сторона:

 public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        [System.Web.Services.WebMethod]
        public static string Delete()
        {
            return "12334";
        }
    }

web.config

<?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" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization" />
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
      </controls>
    </pages>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>      
    </assemblyBinding>
  </runtime>
  <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=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>

Кстати, я также обнаружил те же проблемы в переполнении стека, но это не работает и для меня.

КАК следующие фотографииполные шаги
Шаг 1:
enter image description here Шаг 2:
enter image description here Шаг 3: добавить webform.aspx enter image description here Шаг 3: чтобы вставить код, я не знаю почему или что не так на самом деле это очень простой код для использования AJAX подключиться к серверу, пожалуйста, помогите мне большое спасибо

1 Ответ

0 голосов
/ 31 мая 2018

Попробуйте, это решит вашу проблему
Шаг 1: добавить RouteConfig.cs в папку App_Start

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Off;
        routes.EnableFriendlyUrls(settings);
    }
}

Шаг 2: добавить Global.asax.cs

Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}
...