Answer the question
In order to leave comments, you need to log in
Why does CORS restrict access to the POST action?
Hello. There is a WEB API service on .net core 2.1, which has the action of saving the model from the form:
[HttpPost("[action]")]
public async Task<IActionResult> Save([FromForm] Document document)
{
//Некоторые действия.
}
function SubmitForm(formData) {
let xhr = new XMLHttpRequest();
let form = new FormData(formData);
xhr.open('POST', 'http://localhost:57803/api/save');
xhr.onload = () => {
if (xhr.status == 200) {
alert('Все норм!');
}
else {
alert('Произошла ошибка!');
}
};
xhr.send(form);
};
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors(builder => { builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); });
app.UseMvc();
}
[HttpDelete("[action]/{id}")]
public async Task<IActionResult> Delete(int id)
{
//Некоторые действия.
}
function deleteDoc(id, documentName) {
let xhr = new XMLHttpRequest();
xhr.open('DELETE', 'http://localhost:57803/api/delete/' + id);
xhr.onload = () => {
if (xhr.status == 200) {
deleteRow(id, documentName);
}
else {
errorDelete();
}
};
xhr.send();
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question