C# 在我发送Ajax帖子时未经授权获取401
我一直在Visual Studio 2015~2017中使用post类型测试非常简单的Ajax代码,C# 在我发送Ajax帖子时未经授权获取401,c#,asp.net,ajax,web-services,unauthorized,C#,Asp.net,Ajax,Web Services,Unauthorized,我一直在Visual Studio 2015~2017中使用post类型测试非常简单的Ajax代码, 不幸的是,我总是从浏览器中看到错误消息,如下图所示 请帮我找出这个问题的症结所在,谢谢 附言: 客户端 <script src="Scripts/jquery-1.10.2.min.js"></script> <script> $.ajax({ type: "POST", url: "WebForm1.aspx
不幸的是,我总是从浏览器中看到错误消息,如下图所示
请帮我找出这个问题的症结所在,谢谢 附言:
客户端
<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>
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=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
</configuration>
第二步:
步骤3:添加webform.aspx
第三步:为了把代码放进去,我不知道为什么或者哪里出了问题。事实上,使用ajax连接到服务器是非常简单的代码,请帮助我非常感谢。尝试一下,它会解决您的问题
步骤1:将RouteConfig.cs添加到应用程序的“开始”文件夹中
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);
}
试试看,它会解决你的问题
步骤1:将RouteConfig.cs添加到应用程序的“开始”文件夹中
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);
}
尝试禁用所有类型的身份验证。您可以查看此答案以获得快速(但不推荐)修复。一旦我们确定身份验证确实是个问题,我们就可以从那里着手。您好@RamanPreetgh非常感谢您的推荐,但我没有在我的项目中设置任何内容,因此不应该在权威声明中发生。Hi@RamanpreetSingh,我在内容中加入了更多细节,希望它能让你知道发生了什么,谢谢你能做一件事,将此ajax调用包装到javascript函数中。在页面上放置一个并调用按钮上的javascript函数,单击并查看是否仍然出现此错误。要搜索服务器端问题,这不是客户端问题。因此,VS可以设置在您的项目中尝试禁用各种身份验证。您可以查看此答案以获得快速(但不推荐)修复。一旦我们确定身份验证确实是个问题,我们就可以从那里着手。您好@RamanPreetgh非常感谢您的推荐,但我没有在我的项目中设置任何内容,因此不应该在权威声明中发生。Hi@RamanpreetSingh,我在内容中加入了更多细节,希望它能让你知道发生了什么,谢谢你能做一件事,将此ajax调用包装到javascript函数中。在页面上放置一个并调用按钮上的javascript函数,单击并查看是否仍然出现此错误。要搜索服务器端问题,这不是客户端问题。这样VS就可以在您的项目中进行设置