【发布时间】:2017-05-03 16:05:13
【问题描述】:
使用 WebAPI 后端服务器处理一个新项目,我在从实际网站向控制器发布时遇到问题,尽管 Postman 向控制器发布没有问题。我收到错误 415,浏览器控制台日志记录:
HTTP415: UNSUPPORTED MEDIA TYPE - The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method.
(XHR)OPTIONS - http://localhost:5000/api/Users
虽然来自 Kestrel 的日志是
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 OPTIONS http://localhost:5000/api/Users 0
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 OPTIONS http://localhost:5000/api/Users 0
info: Microsoft.AspNetCore.Mvc.StatusCodeResult[1]
Executing HttpStatusCodeResult, setting HTTP status code 415
Microsoft.AspNetCore.Mvc.StatusCodeResult:Information: Executing HttpStatusCodeResult, setting HTTP status code 415
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action SchoolsChat.Controllers.UserContoller.Post (SchoolsChat) in 2.5323ms
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action SchoolsChat.Controllers.UserContoller.Post (SchoolsChat) in 2.5323ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 6.6615ms 415
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 6.6615ms 415
我正在尝试发布以下 JSON:
{
UserDOB: "2016-12-18",
UserFirstName: "asdf",
UserLastName: "asasdf",
UserName: "asdf",
UserSecret: "asdf"
}
使用这个 TypeScript 类
/**
* JsonPost
*/
class JsonPost {
private _response: number;
public get Reponse(): number {
return this._response;
}
constructor(link: string, data: Object) {
let request = new XMLHttpRequest();
request.withCredentials = true;
request.open("POST", APIUri + link, true);
request.setRequestHeader("content-type", "application/json");
request.setRequestHeader("cache-control", "no-cache");
request.onreadystatechange = () => this._response = request.status;
console.log(request);
request.send(JSON.stringify(data));
}
}
User的模型是
public class User
{
[KeyAttribute]
public int UserId { get; set; }
[RequiredAttribute]
public int SchoolId { get; set; }
[RequiredAttribute]
public string UserName { get; set; }
[RequiredAttribute]
[DataTypeAttribute(DataType.Password)]
public string UserSecret { get; set; } // Unnecessary due to school linking?
[RequiredAttribute]
public virtual School UserSchool { get; set; }
}
虽然发布的控制器很简单,只是输出名字
[HttpPost]
public IActionResult Post([FromBody]User user)
{
Console.WriteLine(user.UserName);
return StatusCode(200);
}
编辑 虽然 nemec 的回答有助于解决问题,但我发现对于 WebAPI,最好的解决方案是仅使用 app.UseCors 作为服务配置 cors。在许多情况下,AddCors 实际上并没有在响应中包含必要的标头。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseCors(options => options.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials());
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
【问题讨论】:
-
请参阅 stackoverflow.com/questions/8153832/… 了解为什么您看到的是
OPTIONS请求而不是POST。
标签: c# asp.net typescript asp.net-core xmlhttprequest