を使用してASP.NetコアWebAPIのに投稿するときに、私は、コントローラへの投稿は問題がないポストマンにもかかわらず、トラブルの実際のWebサイトからコントローラへの投稿を持っています。これを使用してエラー415は、WebAPIのバックエンドサーバーを使用して、新しいプロジェクトに取り組んでのXMLHttpRequest
{
UserDOB: "2016-12-18",
UserFirstName: "asdf",
UserLastName: "asasdf",
UserName: "asdf",
UserSecret: "asdf"
}
:ケストレルからのログは、私は、次のJSONを投稿しようとしています
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
であるが
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
:私は、エラー415、ブラウザのコンソールログを取得しますタイプスクリプトクラス
/**
* 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));
}
}
Tユーザーの彼のモデル掲示用のコントローラは単純であるが
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のからの答えは、問題を解決するにhelpefulれたが、私が見つけましたそれはWebAPIのために特別に最適な解像度は、多くの場合services.AddCorsが実際に応じて、必要なヘッダが含まれていなかったとしてだけapp.UseCorsを使用してCORSを設定することでした。
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();
}
を参照してくださいにhttp:// stackoverflowの。あなたが 'POST'の代わりに' OPTIONS'要求を見ている理由については、/ com/questions/8153832/xmlhttprequest-changes-post-to-optionを参照してください。 –