我正在尝试将post请求从Angular前端发送到ASP.NET后端。
Get请求工作正常。
当我发送:
this.http.post(“http://localhost:3000/api/cards”,“some data”,httpOptions).subscribe(res=>this.display(res));
对于我正在从VS2017运行的ASP.NET API,我得到:
错误{…}错误:Object{Message:“请求的资源不支持http方法”POST“。”}Headers:Object{NormalizedNames:Map(0),LazyUpdate:null,lazyInit:lazyInit()}Message:“http failure response for http://localhost:3000/api/cards:405 method not Allowed”Name:“HttpPerrorResponse”OK:False状态:405 StatusText:“method not Allowed”URL:“http:
对于Angular,“httpoptions”包括:
const httpOptions={headers:new HttpHeaders({“content-type”:“text/plain”,“accept”:“text/plain”})};
我的webapi.config:
<configuration>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Headers" value="Content-type"/>
<add name="Access-Control-Allow-Methods" value="POST"/>
<add name="Access-Control-Allow-Origin" value="*"/>
</customHeaders>
</httpProtocol>
<handlers>
<remove name="WebDAV"/>
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web,Version=2.0.0.0, Culture=neutral,PublicKeyToken=<REMOVED>"/>
<add name="UrlRoutingModule-4.0" path="*" verb="*" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
<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>
<modules runAllManagedModulesForAllRequests="true">
<remove name="TelemetryCorrelationHttpModule" />
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
<remove name="WebDAVModule"/>
<remove name="UrlRoutingModule"/>
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.1" newVersion="4.0.2.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
</configuration>
它看起来可能非常混乱,因为我已经尝试了许多不同的堆栈溢出问题和ASP.NET论坛帖子的解决方案。似乎没有什么可以摆脱405错误。
webapiconfig.cs:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.EnableCors();
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
控制器:
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class CardsController : ApiController
{
// Test data set
private Card[] cards = new Card[]
{
new Card {Id = 1, Suit = "D", Value = "7"},
new Card {Id = 1, Suit = "D", Value = "K"},
new Card {Id = 1, Suit = "S", Value = "A"},
new Card {Id = 1, Suit = "H", Value = "2"},
new Card {Id = 1, Suit = "S", Value = "10"}
};
// GET: api/Cards
[ResponseType(typeof(IEnumerable<Card>))]
public IEnumerable<Card> Get()
{
return cards;
}
// GET: api/Cards/5
public string Get(int id)
{
return "value";
}
// POST: api/Cards
public HttpResponseMessage Post(string value)
{
return Request.CreateResponse("You found me!");
}
// PUT: api/Cards/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE: api/Cards/5
public void Delete(int id)
{
}
}
如果我漏了什么重要的东西就告诉我。
您应该在ASP.NET中的端点上添加[HttpPost]
,如下所示:
[HttpPost]公共操作结果MyAction(){}
我是新来的vuejs和我下面的教程(https://www.youtube.com/watch?v=Wy9q22isx3U
我正在尝试使用ajax发送post请求,但始终出现以下错误: XMLHttpRequest无法加载http://192.168.1.123:8080。对预检请求的响应无法通过权限改造检查:请求的资源上不存在“访问控制允许源”标头。因此不允许访问源“http://localhost:8080”。 这是我的代码
我正在学习春靴和反应现在。 book.js bookresourceimp.java
我想向soap请求发送XML,我使用python请求,如下所示: 当我发送请求时,我会发出以下请求: 我在这样的请求中进行: 我给出了一些错误,比如你不能发送这样的请求。 我意识到我必须发送带有此请求的证书才能获得成功响应 我有<代码>。cer和。crt文件发送,但我这样发送: 并得到以下错误: 更新: 我在PK中使用openssl x509-inform der。cer-输出PK。pem并尝试以
我正在寻找200 OK请求。
我想从php发送post请求到python并得到答案,我写了这个脚本发送post 我使用定制python服务器