gpt4 book ai didi

c# - 发送 Ajax POST 时获得 401 Unauthorized

转载 作者:行者123 更新时间:2023-12-02 14:59:51 26 4
gpt4 key购买 nike

我一直在 Visual Studio 2015~2017 中使用 post 类型测试非常简单的 Ajax 代码,
不幸的是,我总是收到来自浏览器的错误消息,如下图所示
enter image description here

请帮我看看这个问题出了什么问题,谢谢

附言:
客户端

<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";
}
}

网络配置

<?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 也发现了同样的问题,但它对我也不起作用。

如下图为完整步骤
第一步:
enter image description here第二步:
enter image description hereStep3:添加webform.aspx enter image description here第3步:将代码放入,我不知道为什么或有什么问题实际上使用ajax连接到服务器的代码非常简单,请帮助我非常感谢

最佳答案

试试吧,它会解决你的问题
第一步:在你的App_Start文件夹中添加RouteConfig.cs

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

第二步:添加Global.asax.cs

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

关于c# - 发送 Ajax POST 时获得 401 Unauthorized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50616841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com