首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在我发送Ajax帖子时获得401未授权

在我发送Ajax帖子时获得401未授权
EN

Stack Overflow用户
提问于 2018-05-31 12:51:11
回答 1查看 3.5K关注 0票数 1

我在Visual Studio 2015~2017中使用post类型测试了非常简单的Ajax代码,

不幸的是,我总是收到来自浏览器的错误信息,如下图所示

请帮我找出这个问题的原因,谢谢

PS:

客户端

代码语言:javascript
复制
<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>

服务器端:

代码语言:javascript
复制
 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

代码语言:javascript
复制
<?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>

顺便说一句,我在Stack Overflow上也发现了同样的问题,但它对我来说也不适用。

下面的图片是完整的步骤

Step1:

Step2:

Step3:添加webform.aspx

Step3:把代码放进去,我不知道为什么或者出了什么问题其实使用ajax连接服务器是非常简单的代码,请帮助我,谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-31 17:19:49

试试看,它会解决你的问题

Step1:在App_Start文件夹中添加RouteConfig.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50616841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档